[Tutor] a problem wiht struct module

Kent Johnson kent37 at tds.net
Thu Jul 7 13:55:59 CEST 2005


Mohammad Moghimi wrote:
> Hi there
> can you describe why last two lines print different numbers
> 
> ---------------------------------------------------------------
> import struct
> struct.calcsize("lH")
> struct.calcsize("Hl")
> ---------------------------------------------------------------

Because by default struct uses "native" alignment. Your processor presumably aligns longs on long boundaries so two pad bytes are inserted in the Hl case. You can force no alignment by using "standard" alignment. For example on Windows:

 >>> from struct import calcsize
 >>> calcsize('Hl')
8
 >>> calcsize('lH')
6
 >>> calcsize('=Hl')
6
 >>> calcsize('=lH')
6

Kent



More information about the Tutor mailing list