This is the mail archive of the
mailing list for the GCC project.
Re: Pragma question
- To: gcc-help at gcc dot gnu dot org
- Subject: Re: Pragma question
- From: "Lewin A.R.W. Edwards" <larwe at larwe dot com>
- Date: Tue, 20 Feb 2001 12:12:54 -0500
>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
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
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
=== Lewin A.R.W. Edwards (Embedded Engineer)
Personal: http://www.zws.com/ and http://www.larwe.com/
"Und setzet ihr nicht das Leben ein,
Nie wird euch das Leben gewonnen sein."