PDA

View Full Version : quick c question




andrewface
Apr 13, 2007, 08:34 PM
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
Apr 13, 2007, 09:04 PM
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
Apr 13, 2007, 09:08 PM
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
Apr 13, 2007, 09:22 PM
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
Apr 13, 2007, 10:12 PM
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
Apr 13, 2007, 10:26 PM
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.