Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!dptg!att!chinet!john From: john@chinet.chi.il.us (John Mundt) Newsgroups: comp.lang.c Subject: Re: sizeof struct Message-ID: <9505@chinet.chi.il.us> Date: 7 Sep 89 01:02:38 GMT References: <29722@pbhya.PacBell.COM> <2951@cbnewsc.ATT.COM> Reply-To: john@chinet.chi.il.us (John Mundt) Distribution: na Organization: Chinet - Public Access Unix Lines: 60 In article <2951@cbnewsc.ATT.COM> hartman@cbnewsc.att.com (mark.a.hartman) writes: >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. >>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? > >The AT&T 3B20 compiler rounds structures up to an even word length, >in your case from 42 bytes to 44. If you need code that is portable >between the two machines, try adding something like > > char fill[2]; > >to the end of the structure to fill it out. Then, "sizeof struct" >will be the same on both machines. That is no guarantee that the two structures will be portable between both machines. Granted, both will be the same length, but the offset to different members of the structure can vary. I did not see the original structure, so if it was something like struct foo { char fee[42]; }; there would be no problem, but also no need to have a structure. However, if it were something as simple as struct foo { int x; char y[38]; }; y could be offset 2 or 4 bytes from the beginning of the structure. Further, if it were struct foo { char y[37]; int x; }; then x could conceivably be at an offset of 37, 38, 40, or 48 depending on the system, the size of the word, and where different types must be located. Moving structures from disk to different machines is not always so straight forward. Much easier to convert to ascii delimited strings and back again. -- --------------------- John Mundt Teachers' Aide, Inc. P.O. Box 1666 Highland Park, IL john@chinet.chi.il.us (312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem