struct calcsize discrepency?
Nobody
nobody at nowhere.com
Tue Dec 6 03:55:16 EST 2011
On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote:
>> May be, yes, but since calcsize() is returning 12 when the elements
>> are put in the other order, it would seem to be not counting such
>> padding.
>
> Indeed. That's arguably a bug in the struct module,
There's no "arguably" about it. The documentation says:
Native size and alignment are determined using the C compiler’s sizeof
expression.
But given:
struct { unsigned long long a; char b[4]; } foo;
struct { char b[4]; unsigned long long a; } bar;
sizeof(foo) will always equal sizeof(bar). If long long is 8 bytes and has
8-byte alignment, both will be 16.
If you want consistency with the in-memory representation used by
C/C++ programs (and the on-disk representation used by C/C++ programs
which write the in-memory representation directly to file), use ctypes;
e.g.:
>>> from ctypes import *
>>> class foo(Structure):
_fields_ = [
("a", c_ulonglong),
("b", c_char * 4)]
>>> sizeof(foo)
16
More information about the Python-list
mailing list