sizeof struct

Raymond Dunn ray at philmtl.philips.ca
Fri Sep 8 10:34:21 AEST 1989


In article <10945 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <29722 at pbhya.PacBell.COM> afh at PacBell.COM (Alan Hobesh) writes:
>>Why is there a difference [of sizes of struct on different machines]
>
>Both sizes are correct.  The difference is due to structure padding
>(probably at the end of the structure, in this particular case).
>C implementations differ in the amount and location of such padding.
>If you intended the structure to describe an externally-imposed data
>format, you need to be aware that there may be padding between the
>structure members as well as at the end, so a simple roll-in of the
>data into such a structure variable may not work right.

In MS 'C' the compiler can be made to pack structures tightly, at the expense
of the code space and execution time required to access the fields in a less
efficient manner.

In MSC 4.x, use the /Zp option switch. (see page 193 of the Users' Guide).

In MSC 5.x, use the /Zpn option switch or #pragma pack(n), where n defines 1,
2 or 4 byte boundaries.  (see page 100 of the Users' Guide).

Whether or not it's proper to use such language extensions to handle
externally imposed data formats is a discussion I'd rather not breach....

-- 
Ray Dunn.                    | UUCP: ..!uunet!philmtl!ray
Philips Electronics Ltd.     | TEL : (514) 744-8200  Ext: 2347
600 Dr Frederik Philips Blvd | FAX : (514) 744-6455
St Laurent. Quebec.  H4M 2S9 | TLX : 05-824090



More information about the Comp.lang.c mailing list