[Tutor] struct question

Kent Johnson kent37 at tds.net
Tue Feb 3 20:57:46 CET 2009


On Tue, Feb 3, 2009 at 2:03 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:

> More results to chew on:
>
>>>> struct.calcsize('s')
> 1
>>>> struct.calcsize('d')
> 8
>>>> struct.calcsize('sd')
> 16
>>>> struct.calcsize('ds')
> 9
>>>> struct.calcsize('dss')
> 10
>>>> struct.calcsize('dssssss')
> 14
>>>> struct.calcsize('dsd')
> 24
>
> I believe it's a matter of alignment:

Right. The struct module uses native alignment unless you tell it
otherwise. Native alignment is the same as if you defined a C struct
for the given items. In this case the double must start on an 8-byte
boundary.

If you don't want padding, use 'standard' alignment by specifying one
of the modifier prefixes, e.g. '!s!d'

Kent


More information about the Tutor mailing list