Return to the archive index

Re: Memory allocation questions

To: linux-8086@vger.rutgers.edu
Cc: "ELKS List" <linux-8086@vger.rutgers.edu>
From: Perry Harrington <pedward@sun4.apsoft.com>
Date: 	Sat, 12 Jul 1997 20:37:53 -0700 (PDT)
Newsgroups: linux.dev.8086

Ok, to settle this once and for all.  The code below is not unique
to any compiler, it's the standard C way of doing stuff.  Take
this example

struct foo {
	int one;
	char two[15];
};

void main(void)
{

	/* the next line makes the compiler add a stack entry of the
		size of a pointer, commonly 4 bytes in 32 bit model, 2
		in 16 bit */
	struct foo *ptr;

	/* now, the "sizeof" command is a compiler preprocessor usually,
		it looks in it's symbol table for the size of the
		data item and replaces the call with a constant, this
		works because structs can't be declared at runtime.
		Now, Microsoft and Borland diverge on this point,
		Borland treats it as a compiler command, and Microsoft
		allows it to be used to determine the size of a chunk
		of memory, which technically AFAIK is incorrect */

	ptr=malloc(sizeof(struct foo));

}

The above illustrates a rudimentary principal of C.

--Perry

> 
> David Murn wrote:
> > 
> > The reason I ask this, is that I've seen code before in which the
> > programmer has said:
> > 
> >   ptr=malloc(sizeof((struct)foo));
> > 
> > Davey
> 
> Uhh..why don't you code up an example and figure it out yourself, it
> would take all of two minutes if you know anything about what you're
> doing.
> 	Paul 
> 	clements@cs.sc.edu
> 

-- 
Perry Harrington       Linux rules all OSes.    APSoft      ()
email: perry@apsoft.com 			Think Blue. /\

From Unofficial Linux-8086 Mailing List Archive (ULMLA)
Maintained by Robert
Robert's Mailing List Archive Page

Archive created with babymail