w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Can C++ array end at memory boundary?

The standard states explicitly what happens when you increment the pointer to the last element. It gives you a value that can only be used as comparison to check if you're at or before the end of the array or not. The pointer may well point to validly allocated memory for some other object, but that is complete undefined (implementation defined?) behaviour and using that pointer as such is definitely undefined behaviour.

What I'm getting at is that the one-past-the-end pointer is just that: it is the pointer you get when you increment the pointer to the last element, to mark the end of the array in a very cheap way. But do note that comparing pointers of unrelated objects is completely nonsensical (and even undefined behaviour if I'm not mistaken). So the fact that there might be overlap in pointer "values" across different objects is a non-issue, as in exploiting this you enter the Land of Undefined Behaviour..

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.