struct size confusion:

Diez B. Roggisch deets at
Wed Mar 22 15:31:09 CET 2006

Michael Yanowitz wrote:
>    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?

>From TFM:

 Native size and alignment are determined using the C compiler's sizeof
expression. This is always combined with native byte order.

 Standard size and alignment are as follows: no alignment is required for
any type (so you have to use pad bytes); short is 2 bytes; int and long are
4 bytes; long long (__int64 on Windows) is 8 bytes; float and double are
32-bit and 64-bit IEEE floating point numbers, respectively

See this how to achieve the desired results (on my system at least):

>>> print struct.calcsize("I")
>>> print struct.calcsize("H")
>>> print struct.calcsize("HI")
>>> print struct.calcsize("=HI")
>>> print struct.calcsize("=IH")



More information about the Python-list mailing list