[Tfug] OT: sizeof *part* of a struct

Mike Fitzgibbon mikef at lpl.arizona.edu
Thu Apr 1 14:24:21 MST 2010


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)))

On Thu, 1 Apr 2010, Bexley Hall wrote:

> Hi Mike,
>
> > If payload is always at the end, try offsetof(Foo, payload).
>
> Yeah, I thought of that but it isn't extensible.  E.g., imagine if
> there were lots of other cruft in the Foo -- peers with payload.
> You might want to pick and choose which sizeof's to include in
> an expression (without forcing all of them to be contiguous *and*
> at certain places in the struct)
>
> > Otherwise it's probably easiest to define a type for
> > payload:
> > typedef union {
> >    stuff
> >    otherstuff
> > } Payload;
>
> That's what I've done as a workaround.  But, it adds extra
> clutter as each of these "peers" now needs to be promoted to a
> first-class type *just* to get its size...  :<
>
> > On Thu, 1 Apr 2010, Bexley Hall wrote:
> >
> > > Hi,
> > >
> > > I'm trying to find safe syntax for the following:
> > >
> > > typedef struct foo {
> > >   blah-blah-blah;
> > >   union {
> > >      stuff
> > >      otherstuff
> > >   } payload;
> > > } Foo;
> > >
> > > #define OVERHEAD (sizeof(Foo) - sizeof(Foo.payload))
> > >
> > > Suggestions?
> > >
> > > Thx,
> > > --don
> > >
> > >

-- 
Mike





More information about the tfug mailing list