C++ Maximum valid memory address

Viewed 4036

I often see code that adds a value, such as a length to a pointer, and then uses this value, e.g.

T* end = buffer + bufferLen;//T* + size_t

if (p < end)

However, is it possible for the buffer to have been allocated near enough the end of memory that "buffer + bufferLen" may overflow (e.g. 0xFFFFFFF0 + 0x10), resulting in "p < end" being false even if p was a valid element address (e.g. 0xFFFFFFF8).

If it is possible, how can it be avoided when I see many things that work with a begin/end range where end next element after the last one

4 Answers
Related