Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!gem.mps.ohio-state.edu!ginosko!cg-atla!fredex From: fredex@cg-atla.UUCP (Fred Smith) Newsgroups: comp.lang.c Subject: Re: sizeof struct Keywords: sizeof struct Message-ID: <7622@cg-atla.UUCP> Date: 6 Sep 89 12:59:55 GMT References: <29722@pbhya.PacBell.COM> Reply-To: fredex@cg-atla.UUCP (Fred Smith) Distribution: na Organization: Agfa Compugraphic Division Lines: 37 In article <29722@pbhya.PacBell.COM> afh@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