B
BigMan
I wonder if the C++ standard says what should happen if I increment a
pointer to a one-past-the-end value.
I think it says that it is perfectly legal to increment a pointer to
the last element element in an array - I get a pointer to
one-past-the-end value. It is guaranteed that such a value exists for
any array, so I always get a valid pointer to something (I do not care
what it is).
So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:
int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the array
was allocated dynamically?
}
pointer to a one-past-the-end value.
I think it says that it is perfectly legal to increment a pointer to
the last element element in an array - I get a pointer to
one-past-the-end value. It is guaranteed that such a value exists for
any array, so I always get a valid pointer to something (I do not care
what it is).
So, my question is what if I go further and increment a pointer to a
one-past-the-end value? Do I still get a valid pointer to anything?
Let me illustrate my question with a piece of code:
int main
(
)
{
int a[ 2 ];
int* p = a;
++p; // OK, points to the second element (indexed 1).
++p; // OK, points to one-past-the-end value.
++p; // What happens here? Is p a valid pointer? And what if the array
was allocated dynamically?
}