C++ is it legal to take the address of 2 or more past the end of the array?

Viewed 239

From Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

It seems that there is language specific to taking the address of one more than an array end.

Why would 2 or 2,000,000 past the end be an issue if it's not derefferenced?

Looking at some simple loop:

int array[];
...
for (int i = 0: i < array_max; ++i)
{
       int * x = &array[i *2];      // Is this legal
       int y=0;
       if (i * 2 < array_max)       // We check here before dereference
       {
              y = *x;               // Legal dereference
       }
       ...
}

Why or at what point does this become undefined, in practice it just sets a ptr to some value, why would it be undefined if it's not refferenced?

More specifically - what example of anything but what is expected to happen could there be?

3 Answers
Related