Wednesday, October 01, 2008

How does struct.calcsize work?

>>> import struct
>>> struct.calcsize("b") + struct.calcsize("ffi") == struct.calcsize("bffi")
False
I would have expected calcsize("bffi") to be 13, instead it is 16. Hmmm.

Because of this, I have to write some weird code.
>>> a, = struct.unpack("b", buf[0])
>>> b, c, d = struct.unpack("ffi", buf[1:])
Why would this be so...

6 comments:

bins said...

well... that'sb/c of padding and alignement...

this struct (using a better arangement of fields) has size 13:

>>> s.calcsize("ffib")
13

http://en.wikipedia.org/wiki/Data_structure_alignment

cheers,
sebastien.

黄毅 said...

if you specify the byte order, like "&ltbffi", then packing will not take place.

Fredrik said...

For efficiency reasons, a modern computer stores an N-sized entity at an an address that's a multiple of N (that is, the address of a 32-bit entity is always a multiple of four bytes, etc). By default, the struct module takes that into account when using "native" alignment.

For details, see the second table and the text that follows it on the following page:

http://docs.python.org/lib/module-struct.html

mike barton said...

Why when I was a kid, we had to defragment all our data structures. Had to know all the ins and outs of which data types aligned to what word size to minimize memory usage. Uphill both ways.

Was a time, all we got for christmas were bitfields. And we were happy!

Andrew Dalke said...

You had bitfields? When I wanted more bits I had to pull out my wirewrap tool and put them in myself!

dom said...

The second commentor is right. This "bit" me before.

Popular Posts