sizeof( _variable_ )

Henry Spencer henry at utzoo.uucp
Thu Jul 28 06:05:46 AEST 1988


In article <1264 at bc-cis.UUCP> john at bc-cis.UUCP (John L. Wynstra) writes:
>Later on in the same code I had a reference to sizeof(z) expecting to get 22
>(which is btw what I just now got on the bsd 4.2 vax), but what I got was 24!
>... I should think that sizeof( _variable_ )
>should be the length of the _variable_ not the length of the memory allocated
>to it...

Well, yes and no.  Sizeof has to include any necessary padding, so that
things like "foovector = (foo *)malloc(n * sizeof(foo))" work properly.
The key word is "necessary".  With only char members in the struct, on
most machines there should be no padding needed.  Evidently your compiler
is making some worst-case assumptions about structs and is not going to
the trouble of recognizing your struct as an unusually favorable case.
This is perhaps a bit sloppy but is not a violation of specs, since the
specs don't put any constraints on padding.
-- 
MSDOS is not dead, it just     |     Henry Spencer at U of Toronto Zoology
smells that way.               | uunet!mnetor!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list