struct calcsize discrepency?

Chris Angelico rosuav at gmail.com
Sun Dec 4 10:17:41 EST 2011


On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel <d at davea.name> wrote:
> On 12/04/2011 09:35 AM, Chris Angelico wrote:
>>
>>>>> struct.calcsize("4sQ")
>>
>> 16
>>>>>
>>>>> struct.calcsize("Q4s")
>>
>> 12
>>
>> The eight-byte integer is aligned on an eight-byte boundary, so when
>> it follows a four-byte string, you get four padding bytes inserted.
>> Put them in the other order, and the padding disappears.
>>
> NOT disappears.  In C, the padding to the largest alignment occurs at the
> end of a structure as well as between items.  Otherwise, an array of the
> struct would not be safely aligned.  if you have an 8byte item followed by a
> 4 byte item, the total size is 16.

That's padding of the array, not of the structure. But you're right in
that removing padding from inside the structure will in this case
result in padding outside the structure. However, in more realistic
scenarios, it's often possible to truly eliminate padding by ordering
members appropriately.

ChrisA



More information about the Python-list mailing list