0

我有这个结构,但知道内存中不使用每个第 4 个字节,我需要在内存中正确对齐结构。我不完全确定如何做到这一点,虽然我知道我应该这样做,而且我也知道它需要在哪里发生

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;

是否有某种 c 数据类型,例如

align 2

这告诉编译器跳过接下来的 2 个字节还是什么?有点迷失在这里。

4

1 回答 1

1

您可以使用(惊喜)“对齐”属性,如下所示:

__ 属性 __ ((aligned (2)) //字对齐

xc16 用户指南 sect.8.12 是你的朋友。

于 2015-11-25T22:08:48.383 回答