Bug in struct module?

Tim Peters tim_one at email.msn.com
Tue Aug 17 06:32:46 CEST 1999


[Hoon Yoon]
> >>> calcsize('hd')
> 16
> >>> calcsize('dh')
> 10
> That seems like a bug to me.

Assuming you're running on Win9x/NT, it's functioning as designed, although
the actual size of a double-followed-by-short struct under MSVC's default
rules is 16 instead of 10.  To get the actual total size of a struct under
MSVC's defaults, you need to append "0{x}" to the format, where {x} is the
typecode of the largest of the preceding typecodes:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
IDLE 0.5 -- press F1 for help
>>> import struct
>>> struct.calcsize("hd0d")
16
>>> struct.calcsize("dh0d")
16
>>>

If you'd rather get 10 for each result, use the "native byte order, standard
alignment" format code:

>>> struct.calcsize("=dh")
10
>>> struct.calcsize("=hd")
10
>>>

See the docs for details on that.  If you're not using native C structs, the
"standard alignment" rules are much easier to live with (they never
introduce hidden pad bytes, while native alignment may).

but-first-figure-out-how-the-data-is-layed-out-ly y'rs  - tim






More information about the Python-list mailing list