sizeof struct

Fred Smith fredex at cg-atla.UUCP
Wed Sep 6 22:59:55 AEST 1989


In article <29722 at pbhya.PacBell.COM> afh at PacBell.COM (Alan Hobesh) writes:
>
>
>The following c code prints the size of the definded structure to be 44,
>when compiled on an AT&T 3B20 running UNIX V5.2.1.
>
     stuff deleted
>
>However, when the code is downloaded to a PC and compiled using Turbo C,
>the size of the structure is reported to be 42.
>
>Why is there a difference and which is the correct size?



Welcome to the world of "portability"!  This program reports two different
values because the compilers were implemented by different people.

Also, different processors have different data alignment restrictions. (I
do not know what processor is used in the 3B20, so I cannot speak to it.)

There is no requirement that the elements of a structure be packed
tightly together in memory, i.e., with no spaces between elements.
Obviously the Turbo C compiler is packing it tightly, to save space, while
the Unix compiler on the 3B20 is not. 

Members of the 80x86 family CAN
work with packed structures, but the accessing of those data structures
may be less efficient than if they were not packed, depending on the
particular structure's contents. Some other processors require word
alignment, or alignment on a boundary equal to the size of the item
stored there, or other such restrictions.


Hope this helps a little

Fred



More information about the Comp.lang.c mailing list