0

I have this struct, but knowing that every 4th byte is not used in memory, I need to align the struct correctly in memory. I'm not exactly sure how to do this, though I know that I'm supposed to and I also know where it needs to happen.

typedef struct _vol_meta { 
        uint16_t crc;        // 2 bytes  
        uint8_t ver_major;   // 1 byte
        char pad1;           // need to align here - 1 byte

        uint16_t size;       // 2 bytes
        uint8_t ver_minor;   // 1 byte
        char pad2;           // need to align here  - 1 byte

        uint8_t pagenum;     // 1 byte
        uint8_t rownum;      // 1 byte
        char pad3[2];        // align here - 2 bytes

        uint8_t name[15];    // 15 bytes
        // not sure how I'm supposed to align the array of uint8_t vars?
} VOL_META;

Is there some kind of c data type like

align 2

That tells the compiler to skip the next 2 bytes or something? Kind of lost here.

Eric Diviney
  • 327
  • 2
  • 5
  • 16

1 Answers1

1

You can use (surprise) 'aligned' attribute, like that:

__ attribute __ ((aligned (2)) //word alignment

xc16 user guide sect.8.12 is your friend.

Oleg Mazurov
  • 497
  • 5
  • 10
  • Very nice! Thanks, would you happen to have any idea how I could align that array of integers? Wouldn't every 4th entry have to be empty or something like that? – Eric Diviney Dec 01 '15 at 20:19