Using max_align_t to store a chunk of bytes

前端 未结 1 706
小蘑菇
小蘑菇 2021-01-27 05:35

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

相关标签:
1条回答
  • 2021-01-27 06:26

    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.

    0 讨论(0)
提交回复
热议问题