Xref: news2.mv.net comp.os.msdos.djgpp:3640 From: Shawn Hargreaves Newsgroups: comp.os.msdos.djgpp Subject: Re: Structure size Date: Wed, 8 May 1996 20:00:18 +0100 Organization: The University of York, UK Lines: 35 Message-ID: NNTP-Posting-Host: tower.york.ac.uk Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <3190895C.41C6@tc0.chem.tue.nl> To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp On Wed, 8 May 1996, Gert-Jan Schoenmakers wrote: > I've recently switch from Microsoft C/C++ 7.0 to djgpp 2.0 for > developping signal processing software. Now I have a trouble reading > binary data files which consist of a header and an amount of data. The > recent discussion about the sizeof(int) was a first step in solving the > problems I had (I redifined the int's to short int's) but now I'm stuck. > The MSC compiled program tells me sizeof(struct header)=98 and the DJGPP > compiled program tells me sizeof(struct header)=100.... Can someone help > me out on this one ???? This is the result of gcc padding your structures with dummy bytes. It does this because accessing 32 bit values is much faster if they are aligned on a 4-byte boundary, and 16 bit values if they are at an even address. If your stucture mixes fields of different sizes, some of them may end up aligned wrongly, so extra bytes are added in between fields. If this is a problem, you can use the 'packed' attribute to prevent padding, eg: typedef struct foobar { short foo __attribute__ ((packed)); int bar __attribute__ ((packed)); } foobar; There is a bug in gcc that prevents this working with C++ code, though... /* * Shawn Hargreaves. Why is 'phonetic' spelt with a ph? * Check out Allegro and FED on http://www.york.ac.uk/~slh100/ */