struct: Hl vs lH
do you know what this program prints?
I'm on another computer right now that doesn't have Python installed, but what exactly are you asking?
You are supposed to send a format string, not just any string. Do a help('struct.calcsize') for more detail.
Dietrich, he has clearly read the docs, since he is using valid format strings.
"Hl" is a struct with an unsigned short followed by a long integer.
"lH" is a long integer followed by an unsigned short.
The reason these structs are different sizes is because on most systems the elements of C structs are aligned to fall on word boundaries, for efficiency reasons. On systems with a 4-byte word length this will add padding bytes to force elements to start at offsets with a multiple of 4.
So the first struct contains:
* 2 byte unsigned short at offset 0
* 2 padding bytes at offset 2
* 4 bytes long int at offset 4
total length = 8
The second struct contains:
* 4 bytes long int at offset 0
* 2 byte unsigned short at offset 4
Total length = 6
However if you were to have a C array of the second struct then it would still allocate 8 bytes for each entry, since it would align each array entry on a 4-byte boundary.
The results are completely machine dependent - a 64 bit processor will probably pad everything to 8 byte boundaries, resulting in even more padding bytes being added.
Any good book on C programming will explain all this.
Dave - The Developers' Coach