Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > struct curiosity

Reply
Thread Tools

struct curiosity

 
 
pjcoup
Guest
Posts: n/a
 
      10-15-2009
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
 
Reply With Quote
 
 
 
 
Richard Brodie
Guest
Posts: n/a
 
      10-16-2009

"pjcoup" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


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


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-16-2009
Richard Brodie wrote:

> "pjcoup" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>
>> 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

 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      10-16-2009
On Oct 15, 7:07*pm, pjcoup <(E-Mail Removed)> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-16-2009
En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup <(E-Mail Removed)> 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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-16-2009
En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup <(E-Mail Removed)> 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

 
Reply With Quote
 
pjcoup
Guest
Posts: n/a
 
      10-16-2009
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
curiosity about java.util.Timer Daisy Java 3 09-12-2005 07:21 PM
out of curiosity =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 2 09-12-2005 01:06 PM
ConfigurationManager and web.config and ASP.NET refresh curiosity =?Utf-8?B?SmF5IEhpbHlhcmQ=?= ASP .Net 1 04-05-2005 04:12 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments