Closed amosbird closed 4 years ago
Let's replace to vector.data() + idx
. C++ permits to take an address of one element past the array bound.
C++ permits to take an address of one element past the array bound.
Huh? I don't find that in the standard... It sounds to me that &vector[0]
will always be valid, but it's not for the current libcxx implementation at least.
Using vector subscript operator for vec[size]
or even &vec[size]
is illegal.
Taking the address ptr + size
or &ptr[size]
(pointing to one element after the end of the allocated range) is legal.
The difference is that in first case, operator[]
is called. The second case is pure pointer arithmetic.
And C++ standard has the description of what addresses you can safely compare. It specifically includes one element after the end.
This breaks libcxx debug build.