www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/09/17:07:01

Xref: news2.mv.net comp.os.msdos.djgpp:3640
From: Shawn Hargreaves <slh100 AT york DOT ac DOT uk>
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: <Pine.SGI.3.91.960508194859.16831A-100000@tower.york.ac.uk>
NNTP-Posting-Host: tower.york.ac.uk
Mime-Version: 1.0
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/
 */


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019