[Tfug] OT: sizeof *part* of a struct
John Gruenenfelder
johng at as.arizona.edu
Wed Apr 7 23:27:43 MST 2010
On Thu, Apr 01, 2010 at 02:24:21PM -0700, Mike Fitzgibbon wrote:
>Well, if you're interleaving bits of payload with bits of foo, there's no
>easy way to detangle them. Best thing to do would be to put all the
>payload bits into named unions and/or structures. Then you could have:
>
>typedef struct foo {
> blah-blah-blah;
> union payloadA {
> stuff;
> otherstuff;
> } payloadA;
> blahy-blah;
> struct {
> more-blah;
> struct payloadB {
> morestuff;
> } payloadB;
> } pay-blah;
> blah-blah;
>} Foo;
>
>#define OVERHEAD (sizeof(Foo) -
> (sizeof(union payloadA) + sizeof(struct payloadB)))
Unfortunately, there is no cross-platform method of doing this. How this math
works out is dependent on both the compiler and the platform, and any given
compiler is not guaranteed to do it the same way on different platforms.
This exact question came up while I was working on Palm OS on the m68k
platform. That compiler/CPU pair was nice enough to pack structures so
everything behaved well, but I had problems when trying to use the same
headers/structures on my x86_64 desktop which behaved differently.
I was told at the time that the *only* sure-fire way to make this work on any
platform is to write a function which sets each member of the structure
individually and then define OVERHEAD in the most complete way possible so
that it won't matter if the structure is packed or not.
--
--John Gruenenfelder Systems Manager, MKS Imaging Technology, LLC.
Try Weasel Reader for PalmOS -- http://weaselreader.org
"This is the most fun I've had without being drenched in the blood
of my enemies!"
--Sam of Sam & Max
More information about the tfug
mailing list