This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Pragma question


Hi Sebastien,

>Where can I find a precise description of the semantics of 'pragma pack' 
>in GCC 2.95.2 ? The output of the following code amazes me.
>
>I get: "sizeof(withAttribute): 6, sizeof(withPragma): 8"
>
>Why is the structure padded even with pragma packed(1) ?
>This annoys me a lot because there is no other way I can think of to 
>ensure portability in the alignment of a structure.

Unfortunately, "alignment of a structure in RAM" is inherently non-portable.

This is a complicated problem, but from my reading the 
__attribute__((__packed__)) way is preferred and the pragma is depreciated 
(in fact, the documents I read depreciated _all_ pragmas). It might even 
have been removed in current snapshots (2.95.2 is pretty old).

There are two issues here:

1. The intra-structure padding (removing alignment pad bytes within the 
structure) and,

2. The inter-structure padding (removing alignment pad bytes inserted at 
the tail of the structure to ensure that the next thing in memory will be 
aligned).

 From your test, it isn't possible to deduce whether #pragma is handling 
one or neither issue (it obviously isn't handling both). You could do a 
little experimenting by setting the contents of your struct to known values 
and examining the memory bytewise.

If you are looking for a quick and dirty fix to the problem, and "portable" 
is defined as "portable to most systems that will use gcc", use the 
-fpack-struct switch to gcc. It will remove all padding. However on some 
platforms (e.g. ARM) the resulting code will be quite a lot slower and 
_much_ larger!

=== Lewin A.R.W. Edwards (Embedded Engineer)
Work: http://www.digi-frame.com/
Personal: http://www.zws.com/ and http://www.larwe.com/

"Und setzet ihr nicht das Leben ein,
Nie wird euch das Leben gewonnen sein."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]