我有一些类似于以下内容的代码:
void somefunc(uint64_t val) {
uint8_t *p;
uint8_t *s;
s = calloc( (max_sa_len + sizeof(uint64_t) - 1) / sizeof(uint64_t), sizeof(uint64_t));
// s must be 8 byte aligned here
p = (s + sizeof(uint64_t));
// p must be 8 byte aligned here
*(uint64_t)*p = hton64(val & 0xffff);
...
}
当我编译时,我得到:
file.c:400:10: error: cast increases required alignment of target type [-Werror=cast-align]
有没有一种干净的方法来解决这个警告? s
,因此p
必须与64位地址对齐,这不是真正的错误。我尝试在代码中添加p=__builtin_assume_alinged(p,8);
,但它没有修复错误。
我使用arm32(v7)gcc交叉编译器v 4.7.0
答案 0 :(得分:4)
uint64_t *s_u64 = calloc( (max_sa_len + sizeof(uint64_t) - 1) / sizeof(uint64_t),
sizeof(uint64_t));
s = s_u64; // if you need to address by byte as well
...
s_u64[1] = hton64(val & 0xffff);