public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Pragma question
@ 2001-02-20  7:57 Sebastien.Wailliez
  2001-02-20  9:15 ` Lewin A.R.W. Edwards
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastien.Wailliez @ 2001-02-20  7:57 UTC (permalink / raw)
  To: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 836 bytes --]

Hello,

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.

#include <stdint.h>
#include <iostream>

class withAttribute
{
     uint16_t a;
     uint32_t b;
}__attribute__ ((__packed__));

#define HANDLE_SYSV_PRAGMA
#pragma packed( 1 )

class withPragma
{
     uint16_t a;
     uint32_t b;
};

#pragma pack()

int main()
{
     cout << "sizeof(withAttribute): " << sizeof(withAttribute)
     << ", sizeof(withPragma): " << sizeof(withPragma) << endl;

     return 0;
}

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.

Thanks for helping

S

ébastien E. Wailliez

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Pragma question
  2001-02-20  7:57 Pragma question Sebastien.Wailliez
@ 2001-02-20  9:15 ` Lewin A.R.W. Edwards
  0 siblings, 0 replies; 2+ messages in thread
From: Lewin A.R.W. Edwards @ 2001-02-20  9:15 UTC (permalink / raw)
  To: gcc-help

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."

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-02-20  9:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-20  7:57 Pragma question Sebastien.Wailliez
2001-02-20  9:15 ` Lewin A.R.W. Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).