Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: sizeof( _variable_ )
Message-ID: <1988Jul27.200546.21084@utzoo.uucp>
Organization: U of Toronto Zoology
References: <1264@bc-cis.UUCP>
Date: Wed, 27 Jul 88 20:05:46 GMT

In article <1264@bc-cis.UUCP> john@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@zoo.toronto.edu
