struct size confusion:
fredrik at pythonware.com
Wed Mar 22 15:27:53 CET 2006
Michael Yanowitz wrote:
> I am relatively new to Python and this is my first post on
> this mailing list.
> I am confused as to why I am getting size differences in the following
> >>> print struct.calcsize("I")
> >>> print struct.calcsize("H")
> >>> print struct.calcsize("HI")
> >>> print struct.calcsize("IH")
> Why is it 8 bytes in the third case and why would it be only 6 bytes
> in the last case if it is 8 in the previous?
because modern platforms tend to use an alignment equal to the size of
the item; 2-byte objects are stored at even addresses, 4-byte objects
are stored at addresses that are multiples of four, etc.
in other words, HI is stored as 2 bytes H data plus 2 bytes padding plus
four bytes I data, while IH is four bytes I data, no padding, and 2 bytes
> I tried specifying big endian and little endian and they both have
> the same results.
are you sure? (see below)
> I suspect, there is some kind of padding involved, but it does not
> seem to be done consistently or in a recognizable method.
the alignment options are described in the library reference:
default is native byte order, native padding:
to specify other byte orders, use a prefix character. this also disables
More information about the Python-list