struct curiosity

Peter Otten __peter__ at web.de
Fri Oct 16 07:30:37 EDT 2009


Richard Brodie wrote:

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




More information about the Python-list mailing list