2 ms·
Ah, I forgot that "one past the last element" is excepted. I stand corrected. (Similar tricks, such as &h[-1], are indeed undefined behavior.) Regardless, my
by colanderman 10y ago
Ah, I forgot that "one past the last element" is excepted. I stand corrected. (Similar tricks, such as &h[-1], are indeed undefined behavior.)
Regardless, my point stands that it can give you the wrong alignment, if the elements you're putting after the header have greater alignment requirements than the header itself. E.g.:
#include <stdint.h>
struct foo { uint16_t a,b,c; }
uint64_t *bar(struct foo *x) { return (uint64_t *) &x[1]; }
Gives you a misaligned pointer to an 8-byte object.
#include <stdint.h>
#include <stdint.h>
struct foo { uint16_t a,b,c; uint64_t d[]; }
uint64_t *bar(struct foo *x) { return x->d; }
That gives you a properly aligned pointer.