[Pyrex] A riddle

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Apr 19 05:54:26 CEST 2006


Tomasz Primke wrote:

> My friend told me, that in C the assumption
> 
>   sizeof( some struct ) == sum of sizeof( all struct's attributes )
> 
> is not always met. The sum of struct's attributes sizes must be rounded up 
> to 4.

That's not sufficient -- there could be internal
padding between the members of the struct.

In general there's no portable way of calculating
the size of a struct from that of its members.
You have to ask the C compiler by using sizeof
on the actual struct.

You may be able to get an upper bound by rounding
up the size of each member to your platform's
worst-case alignment (which isn't neccessarily
4, it could be 8 or more depending on the platform)
*before* adding them up.

--
Greg




More information about the Pyrex mailing list