gpt4 book ai didi

编译时的 C 数组对齐检查

转载 作者:太空狗 更新时间:2023-10-29 14:57:10 25 4
gpt4 key购买 nike

我正在尝试在编译时检查 8 的数组对齐方式。这是代码:

// File scope
uint32_t pool[1024];
bool aligned = (((uintptr_t) pool) % 8) == 0;

我得到这个错误:初始化元素在加载时不可计算。但是,当我检查 4 的数组对齐时,我没有得到错误。代码如下:

// File scope
uint32_t pool[1024];
bool aligned = (((uintptr_t) pool) % 4) == 0;

语言:C

工具链:arm-none-eabi-gcc

编译器选项:-mcpu=cortex-m3 -mthumb

为什么会这样?

最佳答案

如果您需要强制执行特定对齐,据报道这适用于“arm-none-eabi-gcc”工具链:

uint32_t pool[1024] __attribute__((aligned(8)));

关于编译时的 C 数组对齐检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com