Search code examples
cautoconf

autoconf: check for offset of a member


I have a structure:

struct foo {
  struct {
     int a;
     int b;
     long len;
     unsigned short c;
     unsigned short d;
  };
  char payload[1024];
} bar;

I want to find out, at configure time, if I need to insert padding to make 'payload' double-aligned.

Autoconf provides AC_CHECK_SIZEOF(type) and AC_CHECK_ALIGNOF(type), but what I really need is something like AC_CHECK_OFFSETOF(type, member). If the reported offset was not double-aligned, I could introduce enough padding to make it so.

I could run a little test program that reported offsetof(struct bar, payload), but I don't want to introduce a run-time check into my build system (we cross-compile not infrequently).


Solution

  • Using a zero length bitfield might solve this problem without autotool tricks.

    struct foo {
      struct {
         int a;
         int b;
         long len;
         unsigned short c;
         unsigned short d;
      };
      int64_t : 0; // or long long whatever integer type is sizeof(double)
      char payload[1024];
    } bar;