Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!bloom-beacon!hstbme.mit.edu!scs From: scs@hstbme.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: sizeof struct Keywords: sizeof struct Message-ID: <14102@bloom-beacon.MIT.EDU> Date: 7 Sep 89 03:01:24 GMT References: <29722@pbhya.PacBell.COM> <2951@cbnewsc.ATT.COM> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Distribution: na Lines: 36 In article <2951@cbnewsc.ATT.COM> hartman@cbnewsc.att.com (mark.a.hartman) writes: >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. If you need code that is portable between machines, don't rig it up so that you depend on things like structure sizes. One way to depend on structure size (and arrangement, and byte order, etc.) is to pass data between the machines (over the network, or on magnetic media) by writing and reading whole structures. A much better approach is to write and read an ASCII, machine- independent format. If you must pad a structure out to some known size, a common technique is to stick it in a union along with a buffer of the desired size: union tblock { /* from memory; can't find tar.c or tar.h */ struct header { char name[100]; char uid[8]; char linktype; } header; char dummy[512]; }; As long as sizeof(struct header) is less than 512, sizeof(union tblock) will be exactly 512. (This used to be the case; one of the ANSI C experts will now point out that some clause in the standard allows a compiler writer to implement things such that sizeof(struct header) could be almost anything.) Steve Summit