Is it well defined in c++ to dereference a one-past-the-end pointer to an array type?
Consider the following code :
#include <cassert>
#include <iterator>
int main()
{
// An array of ints
int my_array[] = { 1, 2, 3 };
// Pointer to the array
using array_ptr_t = int(*)[3];
array_ptr_t my_array_ptr = &my_array;
// Pointer one-past-the-end of the array
array_ptr_t my_past_end = my_array_ptr + 1;
// Is this valid?
auto is_this_valid = *my_past_end;
// Seems to yield one-past-the-end of my_array
assert(is_this_valid == std::end(my_array));
}
Common wisdom is that it's undefined behavior to dereference a one-past-the-end pointer. However, does this hold true for pointers to array types?
It seems reasonable that this should be valid since *my_past_end
can be solved purely with pointer arithmetic and yields a pointer to the first element in the array that would be there, which happens to also be a valid one-past-the-end int*
for the original array my_array
.
However, another way of looking at it is that *my_past_end
is producing a reference to an array that doesn't exist, which implicitly converts to an int*
. That reference seems problematic to me.
For context, my question was brought on by this question, specifically the comments to this answer.
Edit : This question is not a duplicate of Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not? I'm asking if the rule explained in the question also apply for pointers pointing to an array type.
Edit 2 : Removed auto
to make explicit that my_array_ptr
is not a int*
.