struct size confusion:

Diez B. Roggisch deets at nospam.web.de
Wed Mar 22 09:31:09 EST 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")
4
>>> print struct.calcsize("H")
2
>>> print struct.calcsize("HI")
8
>>> print struct.calcsize("=HI")
6
>>> print struct.calcsize("=IH")
6
>>>

Regards,

Diez



More information about the Python-list mailing list