在我的代码中,我有这样的东西:
#include <stdint.h>
typedef struct __attribute__((packed)) {
uint8_t test1;
uint16_t test2;
} test_struct_t;
test_struct_t test_struct;
int main(void)
{
uint32_t *ptr = (uint32_t*) &test_struct;
return 0;
}
当我使用 arm-none-eabi-gcc 编译它时,我收到警告
.\test.c:11:2:警告:将压缩的“test_struct_t”指针(对齐 1)转换为“uint32_t”{aka 'long unsigned int'} 指针(对齐 4)可能会导致未对齐的指针值 [- Waddress-of-packed-member]
谁能告诉我为什么会这样?获取压缩结构成员的地址当然是危险的。但是整个结构本身应该总是对齐的,不是吗?