Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!purdue!decwrl!spar!navtech!mark
From: mark@navtech.uucp (Mark Stevans)
Newsgroups: comp.lang.c
Subject: Re: Optimal structure field ordering
Message-ID: <164@navtech.uucp>
Date: 27 Jun 88 06:44:04 GMT
References: <163@navtech.uucp> <2775@ttrdc.UUCP>
Reply-To: mark@navtech.UUCP (Mark Stevans)
Organization: Navigation Technologies Corp., Sunnyvale, CA.
Lines: 37

In article <2775@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes:
, mark@navtech.uucp (Mark Stevans) writes:
<# Due to the alignment requirements of various processor architectures, the
<# declared order of fields within a C structure can significantly effect the
<# space required to store the structure.  The easy way to save space on almost
<# all C implementations is to sort your structure fields in order of descending
<# size of the field type.  For arrays, just use the base type.
<# A brief example.  The following program:
<# 	typedef struct { char buf[5]; short s; char c; long l; } Biggie;
<# 	typedef struct { short s; long l; char buf[5]; char c; } Smallie;
<# 	main()
<# 	{
<# 		printf("Biggie is %d bytes long, but Smallie is only %d bytes long.\n",
<# 				sizeof (Biggie), sizeof (Smallie));
<# 	}
<# When compiled and run on a Sun-3/50 produces the output:
<# 	Biggie is 14 bytes long, but Smallie is only 12 bytes long.