[Tutor] Struct headspinner

Tim Peters tim.peters at gmail.com
Thu Oct 13 00:08:42 CEST 2005


[Liam Clarke]
> Erm, can someone please aid me? I'm using Windows XP, haven't tested
> this code on Linux yet, but, well watch this...
>
> '<' indicates little-endian, @ indicates native. i is an integer,

Yes x 3.

> q is a long.

No.  q in native mode is C "long long" on Linux, or "_int64" on
Windows.  It's an 8-byte integer in standard modes..  "i" is a 4-byte
integer in standard modes, same as C "int" in native modes.

"l" is the format code for long, but none of your examples use that.

> >>> struct.calcsize('<3i')
> 12

3 std integers x 4 bytes each = 12.

> >>> struct.calcsize('@3i')
> 12

Same thing.

> >>> struct.calcsize('<3iq')
> 20

12 + 1 8-byte standard int = 20.

> >>> struct.calcsize('@3iq')
> 24

_int64 is 8-byte aligned on Windows, so that's 12 bytes + 4 bytes
padding + 8 bytes = 24 bytes.

> >>> struct.calcsize('@4iq')
> 24

Same thing; the additional native i fills the padding in the preceding example.

> Is this a feature I don't understand?

Sorry, I don't know what you're asking.  What specifically surprises you here?

> Is a long preceded by 3 integers really 12 bytes long?

No (and nothing above suggests that it is, either ...).

> Surely Microsoft wouldn't do that?

And they didn't ;-)


More information about the Tutor mailing list