www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/15/01:50:28

Date: Mon, 15 Apr 1996 08:46:50 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Megens SA <sam AT cs DOT vu DOT nl>
Cc: djgpp AT delorie DOT com
Subject: Re: problem with packed structs
In-Reply-To: <Dpv3MF.EwE.0.-s@cs.vu.nl>
Message-Id: <Pine.SUN.3.91.960415084346.3742L-100000@is>
Mime-Version: 1.0

On Sun, 14 Apr 1996, Megens SA wrote:

> I ran into the problem of aligned fields within a struct. I need to pack a
> struct so it will have exactly the memory-layout I expect it to be (as
> mentioned in the FAQ, section 22.9). I tried the example that was given in
> the info-package about __attribute__((packed)), but although that should
> work, it doesn't...
> A small program I wrote to test it:
> 
> <source on>
> 
> struct foo
> {
>     char a;
>     int x[2] __attribute__((packed));
> };

You should declare ALL the fields with the attribute, like this:

	struct foo
	{
	  char a __attribute__((packed));
	  int x[2] __attribute__((packed));
	};

and then it will work--unless you compile it as a C++ source, because GCC 
2.7.2 has a known bug in the C++ compiler that prevents it from packing 
structures.  (There is a work-around using pragmas, but if this is a C 
program, you don't want to know.)

- Raw text -


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