C malloc/pointer question
Mar. 26th, 2006 09:52 pmWhat's the difference between:
and
Namely, the malloc line... I don't understand why one uses sizeof(address-of-variable) and another uses sizeof(variable-type)? Help?
(Yeah, this is probably absurdly basic, but.. I dunno. This is what I get for learning Java before C, and not really fully understanding memory)
Like.. would, "if (NULL == (new_cover = malloc (sizeof (image_cover_t)))) {..." work too...?
image_cover_t* new_cover; /* the new cover *
/* Allocate the new image cover. */
if (NULL == (new_cover = malloc (sizeof (*new_cover)))) {...
and
int *iptr;
iptr = (int *)malloc(10 * sizeof(int));
if (iptr == NULL){...
Namely, the malloc line... I don't understand why one uses sizeof(address-of-variable) and another uses sizeof(variable-type)? Help?
(Yeah, this is probably absurdly basic, but.. I dunno. This is what I get for learning Java before C, and not really fully understanding memory)
Like.. would, "if (NULL == (new_cover = malloc (sizeof (image_cover_t)))) {..." work too...?
no subject
Date: 2006-03-27 07:46 am (UTC)int hi_I_am_an_int_type_variable;
[...]
sizeof(hi_I_am_an_int_type_variable)
returns the same thing.
*new_cover in that first sizeof() is a dereferenced pointer to a type (that's what the * does in this usage, which I consider a confusing part of C), so it is really sizeof(image_cover_t).
no subject
Date: 2006-03-27 07:50 am (UTC)no subject
Date: 2006-03-27 05:21 pm (UTC)