delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/01/28/21:05:59

Date: Sat, 28 Jan 1995 12:01:46 -0800 (PST)
From: Gordon Hogenson <ghogenso AT u DOT washington DOT edu>
To: Jason Hoffoss <hoffo002 AT gold DOT tc DOT umn DOT edu>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Major bug

On Fri, 27 Jan 1995, Jason Hoffoss wrote:

> Then, I put in a debugging line like:
>
>    printf("%d\n", sizeof(header));
> 
> Which gave me 24 for output.  If you look carefully, however, you will see 

That's not a bug.  Nothing in the C standard requires structs to
be layed out in a particular way.  Code that depends on the exact
layout of a structure is inherently non-portable.  GCC aligns
elements of a struct as it sees fit, for efficiency.  If you want a
struct to be packed (i.e. no space in-between elements), you must 
use the __attribute__ ((packed)) notation on each element of the 
structure (see the info pages).

That is a subtle gotcha, though......  I don't know if it's possible to
get gcc to issue a warning in cases like this.

Gordon.


- Raw text -


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