delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/05/29/17:39:30

Date: Fri, 29 May 92 17:08:12 EDT
From: DJ Delorie <dj AT ctron DOT com>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: [mdb AT NSD DOT 3Com DOT COM: Re: packed structures ]
Status: O

FYI - djgpp users often need this capability.  I haven't tried it
myself, though.

DJ

--


From: "Mark D. Baushke" <mdb AT NSD DOT 3Com DOT COM>

We have had need of this in our code. The version of gcc we received from
Cygnus (1.96) allows us to use the '__attribute__ ((aligned (x)))' mechanism
to pack our structures.

The following comments and #define are from one of our standard .h files
for dealing with this trick on various compilers.

	-- Mark

/*
 * The GNU C Compiler version 2.x allows an individual field in a
 * structure to have an altered alignment. 
 *
 * struct foo
 * {
 *     char a __attribute__ ((aligned (1)));
 *     int  b __attribute__ ((aligned (1)));
 * };
 * 
 */
#define packed_struct	struct /* entire structure packing is not for gcc */
#define packed_field	__attribute__ ((aligned (1)))

(Obviously we define packed_struct to be the appropriate magic for each
compiler -- for High-C the packed_struct macro is defined to be
'_packed struct'.)

This is how we use it:

Example:

	packed_struct {
	  short x packed_field;
	  long y packed_field;
	} foo;


	------- original message -------

From: DJ Delorie <dj AT ctron DOT com>
Subject: packed structures

I've gotten some requests for, and personally had need for, the
ability to override alignment on structures to produce packed
structures.  The code in the back-end appears to support this, but the
front end never requests it.  Is this a change (enhancement?) to the
language (C/C++) and/or compiler that can be added without blowing
anything up?

Example:

	packed struct {
	  short x;
	  long y;
	} foo;

where offsetof(foo,y) is 2, not 4.

Obviously, this may not be possible for some processors, but I know
the x86 family can handle unaligned accesses (except for one mode of
the '486)

DJ
dj AT ctron DOT com
Life is a banana.


- Raw text -


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