Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1357

Re: Structure Packing.

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: Structure Packing.
Date 2012-06-06 18:35 -0700
Organization None to speak of
Message-ID <lny5nzdhts.fsf@nuthaus.mib.org> (permalink)
References <12b3df42-f6a0-4411-a5e5-136900f8255b@googlegroups.com>

Show all headers | View raw


John Kearney <dethrophes@googlemail.com> writes:
> Hi hopefully just a quick question.
> struct s{
>    uint16_t var1;
>    uint16_t var2;
> };
>
> would it ever be acceptable for a compiler to align var2 on a 4/8 byte boundary, for speed, architectural or indeed any reason.
>
> I just always feel uneasy when I see code that relies on the above structure being packed together without an explicit pack pragma.
> is this just my paranoia?

I'm not sure just what you mean by "a 4/8 boundary". I'm going to
guess that you mean either 4-byte or 8-byte alignment.

I'll also assume 8-byte bytes (CHAR_BIT==8). The standard requires
CHAR_BIT>=8. Any system you're likely to encounter, unless you work
with DSPs, is almost certain to have CHAR_BIT==8 exactly.

Given that the two members are of the same type, it's very likely that
there will be no padding between them. If you declared an array of
uint16_t, padding between elements is forbidden, so the compiler is
certainly able to generate code to access var1 and var1 if there's no
padding.

Compilers are *permitted* to add arbitrary padding between any
two struct members, or after the last one. The intended purpose
of such padding is to enable better code for accessing members
-- but the standard imposes no such restrictions.  For example,
a conforming compiler could make sizeof (struct s) == 8, with 2
bytes of padding after var1 and another 2 after var2. But it's
unlikely that any real-world compiler would do so.

Bottom line: a compiler *could* add padding, but in practice it's
vanishingly unlikely to do so.

If you're concerned, you could add something like:

    assert(sizeof (struct s) == 4);

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-06 17:27 -0700
  Re: Structure Packing. James Kuyper <jameskuyper@verizon.net> - 2012-06-06 21:19 -0400
    Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-11 22:39 -0400
      Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 09:21 -0400
        Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 11:51 -0400
          Re: Structure Packing. Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-06-12 18:37 +0200
            Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 15:06 -0400
              Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 16:22 -0400
                Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 17:44 -0400
                Re: Structure Packing. Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-06-13 15:53 +0200
                Re: Structure Packing. Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-06-18 12:37 +0300
        Re: Structure Packing. Martin Shobe <martin.shobe@yahoo.com> - 2012-06-12 16:22 +0000
          Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 15:24 -0400
            Re: Structure Packing. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-12 16:33 -0400
              Re: Structure Packing. Walter Banks <walter@bytecraft.com> - 2012-06-12 17:17 -0400
  Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-06 18:35 -0700
    Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-06 21:58 -0700
      Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-06 23:54 -0700
        Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-07 07:47 -0700
          Re: Structure Packing. James Kuyper <jameskuyper@verizon.net> - 2012-06-07 12:39 -0400
    Re: Structure Packing. jacob navia <jacob@spamsink.net> - 2012-06-07 11:14 +0200
      Re: Structure Packing. Keith Thompson <kst-u@mib.org> - 2012-06-07 09:46 -0700
        Re: Structure Packing. John Kearney <dethrophes@googlemail.com> - 2012-06-07 11:57 -0700

csiph-web