Bypass "message about the increase in the number of required alignment"

I have some code that looks something like this:

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);
...
}

And when I compile, I get:

file.c:400:10: error: cast increases required alignment of target type [-Werror=cast-align]

Is there a clean way around this warning? s, and therefore pmust be aligned with 64-bit addresses, this is not a real mistake. I tried to add code p=__builtin_assume_alinged(p,8);to the code, but it did not fix the error. I get this error using arm32 (v7) gcc cross compiler, v 4.7.0

+4
source share
1 answer
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);
+4
source

Source: https://habr.com/ru/post/1693884/


All Articles