On Thu, May 7, 2009 at 12:53 PM, Tyler Littlefield <[email protected]> wrote:
> that's what I was talking about.

No you weren't. Or if you were, you didn't express it correctly.

> he had (structure*)=malloc(64) which means
> that if structure (what ever that may be) changes, it's only going to get 64 
> bytes

Your suggestion was essentially the same as his in certain cases.

If the type of 'storage' changes from 'structure' to (say)
'structure2' which is a different size then using sizeof(structure) in
the malloc isn't going to magically change the number of bytes
allocated.

Or another way of looking at it - it's one more thing that needs to be
changed, when it can be written in such a way that it shouldn't
matter.

Note that this concept is different to merely changing 'structure.'

To make it somewhat clearer, a simpler example:

short* foo;
/* this will change to
long* foo;
some time in the future */

/* lots of lines */

foo = malloc(2);  /* this is what the OP was using - this wouldn't
work as intended on certain platforms before the change, and almost
certainly won't as intended anywhere after. */
foo = malloc (sizeof short);  /* this is what you're suggesting, and
works before the declaration change, but ALSO requires a change to the
malloc after in order to still work as intended. If it gets noticed*/
foo = malloc (sizeof *foo); /* this is what I'm suggesting, and works
either way. */


-- 
PJH

http://shabbleland.myminicity.com/com
http://www.chavgangs.com/register.php?referer=9375

Reply via email to