>>> import structI would have expected calcsize("bffi") to be 13, instead it is 16. Hmmm.
>>> struct.calcsize("b") + struct.calcsize("ffi") == struct.calcsize("bffi")
False
Because of this, I have to write some weird code.
>>> a, = struct.unpack("b", buf[0])Why would this be so...
>>> b, c, d = struct.unpack("ffi", buf[1:])
6 comments:
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.
if you specify the byte order, like "<bffi", then packing will not take place.
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
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!
You had bitfields? When I wanted more bits I had to pull out my wirewrap tool and put them in myself!
The second commentor is right. This "bit" me before.
Post a Comment