HardcoreHenry
HardcoreHenry

Reputation: 6387

Getting around "cast increases required alignment order" message

I have some code which looks similar to the following:

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 to get around this warning? s, and therefore p must be aligned to 64 bit addresses, this it's not a real error. I tried adding a p=__builtin_assume_alinged(p,8); to the code but it did not fix the error. I'm getting this error with an arm32 (v7) gcc cross compiler, v 4.7.0

Upvotes: 5

Views: 1595

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126867

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

Upvotes: 5

Related Questions