In this thread I was suggested to use max_align_t
in order to get an address properly aligned for any type, I end up creating this implementation of a dynamic array
Does it break the one past the last element of an array rule?
No.
Using
max_align_t
to store a chunk of bytes
OP's issue is not special because it uses a flexible array member.
As a special case, the last element of a structure ... have an incomplete array type; this is called a flexible array member. ... However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) ...
It is the same issue as accessing any allocated memory or array of one type as if it was another type.
The conversion from max_align_t *
to char *
to void *
is well defined when alignment is done right.
A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. C11dr §6.3.2.3 7
All reviewed accessing in code do not attempt to access outside the "as if" array.