sizeof( _variable_ )

Alan T. Bowler [SDG] atbowler at watmath.waterloo.edu
Sat Aug 13 11:04:13 AEST 1988


In article <1988Jul27.200546.21084 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>
>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.

The compiler writer may have good reason for deciding to pad "all char"
structs to a word (or double word) boundary.  There are a number of
machines on which the block move instructions will work much faster
if the data is aligned to an appropriate boundary.   It may also be
that it was just a simplifying assumption that reduces the complexity
of the code generator.  Simplifying the compiler is no of itself a
good thing, but it tends to increase compiler speed, and reduce bugs.



More information about the Comp.lang.c mailing list