6

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*.

23
  • 3
    No, that would be UB Commented Oct 9, 2018 at 18:13
  • @πάνταῥεῖ I agree, but if you look at the answers in the linked question you'll find that not everyone does. Commented Oct 9, 2018 at 18:15
  • You might use the pointer value for calculations, though dereferencing would be definitiely UB. // Is this valid? auto is_this_valid = *my_past_end; Definitely not. assert(is_this_valid == std::end(my_array)); That would be still valid though. Commented Oct 9, 2018 at 18:17
  • 4
    @Slava No, referencing the resulting pointer value is valid for calculations, dereferencing it never is valid. Commented Oct 9, 2018 at 18:28
  • 2
    my_array_ptr is a pointer to the whole array, not the first element. It a int(*)[3] not a int *. Commented Oct 9, 2018 at 18:52

2 Answers 2

8

This is CWG 232. That issue might seem like it's mainly about dereferencing a null pointer but it's fundamentally about what it means to simply dereference something that doesn't point to an object. There is no explicit language rule about this case.

One of the examples in the issue is:

Similarly, dereferencing a pointer to the end of an array should be allowed as long as the value is not used:

char a[10];
char *b = &a[10];   // equivalent to "char *b = &*(a+10);"

Both cases come up often enough in real code that they should be allowed.

This is basically the same thing as OP (the a[10] part of the above expression), except using char instead of an array type.

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?

There is no difference in the rules based on what kind of pointer it is. my_past_end is a past-the-end pointer, so whether it's UB to dereference it or not is not a function of the fact that it points to an array as opposed to any other kind of type.


While the type of is_this_valid an int* which gets initialized from a int(&)[3] (array-to-pointer decay), and thus nothing here actually reads from memory - that is immaterial to the way the language rules work. my_past_end is a pointer whose value is past the end of an object, and that's the only thing that matters.

Sign up to request clarification or add additional context in comments.

8 Comments

So is it legal or not?
OK so we can assume it is undefined behavior until issue is solved in either way.
@Slava I don't know why you would say that. Given how much code that exists that does something like this, I would expect that if the wording changes, it would change to allow this.
is it undecided behaviour then?
@Barry because if code has UB or not is not defined by how much code uses it but if this behaviour is defined by standard and it is safer to assume that behaviour is undefined unless proven otherwise.
|
0

I believe it's well defined, because it doesn't dereference the one-past-the-end pointer.

auto is_this_valid = *my_past_end;

my_past_end is of type int(*)[3] (pointer to array of 3 int elements). The expression *my_past_end is of therefore of type int[3] -- so like any array expression in this context, it "decays" to a pointer of type int*, pointing to the initial (zeroth) element of the array object. This "decay" is a compile-time operation. So the initialization simply initializes is_this_valid, a pointer of type int*, to point just past the end of my_array. No memory past the end of the array object is accessed.

5 Comments

Downvoter: It's entirely possible that I'm wrong. If so, can you explain why?
That's just, like, your opinion, man. If you want to show that it's well defined, you should cite the standard.
@melpomene: I'm saying it's well defined because it doesn't dereference a one-past-the-end pointer. Where would the standard describe something that doesn't happen? Array-to-pointer conversion (which is implicit) is defined in [conf.array], 4.2 of the C++11 standard, but I assume that's common knowledge.
Things are undefined by default. For every piece of code with defined behavior, there is a part of the standard that defines what it does. *my_past_end is a use of the * operator, whose description states "the result is an lvalue referring to the object or function to which the expression points". my_past_end does not point to any object, so the behavior is undefined.
*x dereferences x (if the expression is evaluated, which it is). You seem to be incorrectly conflating "dereference x" with "accessing memory where x points"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.