timsort-cpp icon indicating copy to clipboard operation
timsort-cpp copied to clipboard

Type aliasing violation in timsort_stack_buffer?

Open Morwenn opened this issue 7 years ago • 0 comments

Unless I'm mistaken, the reinterpret_cast in timsort_stack_buffer that performs the pointer to integer type -> pointer to type to merge conversion violates the C++ type aliasing rules (see the corresponding section in the cppreference page on reinterpret_cast). Instead of storing an integer array, you could surely store a char array instead an reinterpret the memory as either integer or as storage for the values to merge, suitably aligned with the alignment you already compute today. This would allow to both keep the smart stack buffer optimization while actually not entering Undefined Behaviour territory (the current reinterpret_cast is most likely UB) today even though it happens to work on most compilers).

This is exactly the strategy adopted by Howard Hinnant's stack allocator, so it is most likely standard compliant enough.

Morwenn avatar May 26 '18 11:05 Morwenn