[CentralOH] struct module

Mark Erbaugh mark at microenh.com
Tue Oct 14 02:48:46 CEST 2008


I'm trying to use the struct module to read some data that was created
by saving a C++ struct.  The C++ program was compiled using quad word (8
byte) alignment.

According to the struct docs:

Hint: to align the end of a structure to the alignment requirement of a
particular type, end the format with the code for that type with a
repeat count of zero. For example, the format 'llh0l' specifies two pad
bytes at the end, assuming longs are aligned on 4-byte boundaries. This
only works when native size and alignment are in effect; standard size
and alignment does not enforce any alignment.

struct.calcsize('@h0f') returns 4 (2 byte short, plus padding to 4
bytes) as expected

however,

struct.calcsize('@h0d') also returns 4 bytes. Shouldn't it to return 8
bytes, padding the 2 byte short to 8 bytes.

struct.calcsize('@d') returns 8

This is with:

Python 2.4.3 (#2, Jul 31 2008, 21:56:52) 
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2

Thanks,
Mark





More information about the CentralOH mailing list