delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/15/18:26:36

Sender: nate AT cartsys DOT com
Message-ID: <37B733B0.99A38A54@cartsys.com>
Date: Sun, 15 Aug 1999 14:40:00 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.12pre4 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Er, pardon?
References: <37B71920 DOT 406B AT surfsouth DOT com>
Reply-To: djgpp AT delorie DOT com

Chris Holmes wrote:
> 
> What exactly does __attribute__ ((packed))   tell the compiler to do?
> It just fixed several problems I was having with some stuff, only it
> doesn't make any sense to me as to why that worked.

Imagine a struct like the following:

struct foo {
	char c;
	int i;
};

GCC knows that an x86 will get better performance when an `int' is
aligned on a 4-byte boundary; therefore, it inserts three bytes of
padding between `c' and `i'.  If your program expects (usually because
of trying to use data written in a file) that `i' immediately follows
`c', it will break.  __attribute__((packed)) tells the compiler not to
do this padding, though there is a speed penalty.

It's probably better to write the code in such a way that it's
independent of the way the struct is laid out.  For instance, don't try
to read an entire struct from a file directly; read the individual
members and fill them in.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


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