public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Lewin A.R.W. Edwards" <larwe@larwe.com>
To: gcc-help@gcc.gnu.org
Subject: Re: Pragma question
Date: Tue, 20 Feb 2001 09:15:00 -0000	[thread overview]
Message-ID: <4.3.2.7.2.20010220120458.00a95df0@larwe.com> (raw)
In-Reply-To: <C12569F9.00578DF9.00@bemail05.net.alcatel.be>

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

      reply	other threads:[~2001-02-20  9:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-20  7:57 Sebastien.Wailliez
2001-02-20  9:15 ` Lewin A.R.W. Edwards [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4.3.2.7.2.20010220120458.00a95df0@larwe.com \
    --to=larwe@larwe.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).