struct curiosity

Peter Otten __peter__ at
Fri Oct 16 13:30:37 CEST 2009

Richard Brodie wrote:

> "pjcoup" <pjcoup at> wrote in message
> news:b1537079-6e3a-43e1-814b-7ccf185fb9c3 at
>> 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.


More information about the Python-list mailing list