Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
what does this statement do?

A = malloc(10*sizeof(long));

also another question,

if malloc() fails to allocate memory then it returns NULL right?
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
sizeof returns the size of the type. In the case of long, it is probably 4 for 4 bytes. So A would be 40.

A failure would cause malloc to return a null pointer, ie 0.
 

andrewface

macrumors 6502
Original poster
May 17, 2006
284
56
sizeof returns the size of the type. In the case of long, it is probably 4 for 4 bytes. So A would be 40.

A failure would cause malloc to return a null pointer, ie 0.

and A would be of size 40 in memory right?

hmm
ive got this question on an assignment....

so..

if malloc fails to allocate memory it returns

a) -1
b) NULL
c) EOF
d) '\o'

i thought NULL...what would the answer be?
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
I hate to sound nasty, but I think if you looked for the correct answer to your quiz it would be faster than asking people here. And, you might learn something.

And, I already answered that question in the previous post.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
If you have a Mac with the developer kit installed, just open a terminal session and do a "man malloc"

It will give you the answer.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,337
Los Angeles
and A would be of size 40 in memory right?
To answer this part... Your statement doesn't quite make sense. malloc returns a pointer, i.e., some 32-bit or 64-bit value that is the address of an area of memory. So the type of the variable named "A" is pointer to something. Therefore A itself happens to be 32 bits or 64 bits in size and its value is a 32-bit or 64-bit number that is the address of an area of memory bit enough to hold 10 values of type long.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.