struct curiosity

Discussion in 'Python' started by pjcoup, Oct 15, 2009.

  1. pjcoup

    pjcoup Guest

    Hello,

    I was fooling around with python's struct lib, looking on how we'd
    unpack some data. I was a little confused by its behavior:
    Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10)
    [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import struct
    >>> struct.calcsize('BhhhhB')

    11
    >>> struct.calcsize('@BhhhhB')

    11
    >>> struct.calcsize('<BhhhhB')

    10
    >>> struct.calcsize('>BhhhhB')

    10

    I would have expected calcsize('BhhhhB') to be either 10 or 12
    (padding), but 11?
    Is there a simple explanation of what is going on here? Just a
    curiosity.
    This is on a x86_64, but have seen the same on i686. Ideas?

    Thanks.
    Pete
    pjcoup, Oct 15, 2009
    #1
    1. Advertising

  2. "pjcoup" <> wrote in message
    news:...


    > I would have expected calcsize('BhhhhB') to be either 10 or 12
    > (padding), but 11? Is there a simple explanation of what is going
    > on here?


    The purpose of the padding is to align the words 'naturally'.
    That is, when reading two bytes, to start at an even number.

    B X B h1
    h1 h1 h1 h2
    h2 h2 h2 h3
    h3 h3 h3 h4
    h4 h4 h4 B
    B Y

    The padding at X lines up h1-h4. There isn't any point
    putting padding at Y.
    Richard Brodie, Oct 16, 2009
    #2
    1. Advertising

  3. pjcoup

    Peter Otten Guest

    Richard Brodie wrote:

    > "pjcoup" <> wrote in message
    > news:...
    >
    >
    >> I would have expected calcsize('BhhhhB') to be either 10 or 12
    >> (padding), but 11? Is there a simple explanation of what is going
    >> on here?

    >
    > The purpose of the padding is to align the words 'naturally'.
    > That is, when reading two bytes, to start at an even number.
    >
    > B X B h1
    > h1 h1 h1 h2
    > h2 h2 h2 h3
    > h3 h3 h3 h4
    > h4 h4 h4 B
    > B Y
    >
    > The padding at X lines up h1-h4. There isn't any point
    > putting padding at Y.


    I would have expected "native size and alignment" (as stated in the
    documentation) to mean that I can read the output of struct.pack("bhhhhb")
    into a C

    struct {
    char a;
    short b1, b2, b3, b4;
    char c
    }

    but that has a size of 12 bytes on my 64 bit Ubuntu.

    Peter
    Peter Otten, Oct 16, 2009
    #3
  4. On Oct 15, 7:07 pm, pjcoup <> wrote:
    > I was fooling around with python's struct lib, looking on how we'd
    > unpack some data.  I was a little confused by its behavior:
    > Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10)
    > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> import struct
    > >>> struct.calcsize('BhhhhB')

    > 11
    > >>> struct.calcsize('@BhhhhB')

    > 11


    This result *includes* any necessary padding between two
    consecutive fields of the struct (in this case, the extra
    byte of padding between the first 'B' and the following
    'h', but *excludes* padding at the end of the struct.
    The result of struct.pack with one of these formats should
    have length exactly 11: the struct module doesn't bother
    including the trailing padding.

    > >>> struct.calcsize('<BhhhhB')

    > 10
    > >>> struct.calcsize('>BhhhhB')

    > 10


    The non-native formats don't include padding at all.

    Mark
    Mark Dickinson, Oct 16, 2009
    #4
  5. En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup <> escribió:

    >>>> import struct
    >>>> struct.calcsize('BhhhhB')

    > 11
    >>>> struct.calcsize('@BhhhhB')

    > 11
    >>>> struct.calcsize('<BhhhhB')

    > 10
    >>>> struct.calcsize('>BhhhhB')

    > 10
    >
    > I would have expected calcsize('BhhhhB') to be either 10 or 12
    > (padding), but 11?


    There are no pad bytes following the last member; from the docs:

    """Hint: to align the end of a structure to the alignment requirement of a
    particular type, end the format with the code for that type with a repeat
    count of zero. For example, the format 'llh0l' specifies two pad bytes at
    the end, assuming longs are aligned on 4-byte boundaries. This only works
    when native size and alignment are in effect; standard size and alignment
    does not enforce any alignment."""

    py> calcsize("BhhhhB")
    11
    py> calcsize("BhhhhB0h")
    12

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 16, 2009
    #5
  6. En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup <> escribió:

    >>>> import struct
    >>>> struct.calcsize('BhhhhB')

    > 11
    >>>> struct.calcsize('@BhhhhB')

    > 11
    >>>> struct.calcsize('<BhhhhB')

    > 10
    >>>> struct.calcsize('>BhhhhB')

    > 10
    >
    > I would have expected calcsize('BhhhhB') to be either 10 or 12
    > (padding), but 11?


    There are no pad bytes following the last member; from the docs:

    """Hint: to align the end of a structure to the alignment requirement of a
    particular type, end the format with the code for that type with a repeat
    count of zero. For example, the format 'llh0l' specifies two pad bytes at
    the end, assuming longs are aligned on 4-byte boundaries. This only works
    when native size and alignment are in effect; standard size and alignment
    does not enforce any alignment."""

    py> calcsize("BhhhhB")
    11
    py> calcsize("BhhhhB0h")
    12

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 16, 2009
    #6
  7. pjcoup

    pjcoup Guest

    Yes, this is basically what I expected as well.
    I would have expected some size that you can coax gcc to give, either
    12 (as here), or 10 (with directives).
    Thanks to everyone for the responses!

    Pete


    On Oct 16, 4:30 am, Peter Otten <> wrote:
    >
    > I would have expected "native size and alignment" (as stated in the
    > documentation) to mean that I can read the output of struct.pack("bhhhhb")
    > into a C
    >
    > struct {
    >   char a;
    >   short b1, b2, b3, b4;
    >   char c
    >
    > }
    >
    > but that has a size of 12 bytes on my 64 bit Ubuntu.
    >
    > Peter
    pjcoup, Oct 16, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF5IEhpbHlhcmQ=?=

    ConfigurationManager and web.config and ASP.NET refresh curiosity

    =?Utf-8?B?SmF5IEhpbHlhcmQ=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    901
    Brock Allen
    Apr 5, 2005
  2. =?Utf-8?B?cm9kY2hhcg==?=

    out of curiosity

    =?Utf-8?B?cm9kY2hhcg==?=, Sep 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    897
    =?Utf-8?B?cm9kY2hhcg==?=
    Sep 12, 2005
  3. Daisy
    Replies:
    3
    Views:
    4,067
    Steve Horsley
    Sep 12, 2005
  4. Richard
    Replies:
    2
    Views:
    375
    Jacqui or (maybe) Pete
    Jul 10, 2003
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page