| ▲ | Joker_vD 3 hours ago | |||||||||||||||||||||||||
> Accessing an array out of bounds is UB. There is no OoB access of an array; the calculated pointer is pointing to the payload object that's residing in the malloc-returned storage right after the node struct. I think the actual problem is the alignment; that malloc-returned storage simply can't have enough space to hold a "struct { struct node header; PAYLOAD_TYPE payload; }" (which is what the parent comment is trying to emulate) if the payload type has an alignment that's greater than the size of a pointer, and that pointer will be pointing at what would've been the padding in that struct. | ||||||||||||||||||||||||||
| ▲ | el_pollo_diablo 3 hours ago | parent [-] | |||||||||||||||||||||||||
> There is no OoB access of an array Yes, there is. It does not matter that storage happens to be allocated beyond the end of said array. Strict aliasing implies that it is UB to reinterpret the array as anything else. And it is UB to access an array out of bounds. Flexible array members specifically exist for these dynamically-allocated trailing arrays. They do not solve the strict aliasing problem, though. > if the payload type has an alignment that's greater than the size of a pointer The amount of padding is implementation-defined. The only portable guarantee is that 'payload' is aligned for its element type, char. To over-align, use _Alignas, as in: | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||