I was reading some docs about STL and it was written there that the end()
function returns an iterator of the byte next to the last element of the container.
And I wonder, what if the container occupies the very last bytes of the whole available memory. What will happen then?
The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container.
vector::begin() function is a bidirectional iterator used to return an iterator pointing to the first element of the container. vector::end() function is a bidirectional iterator used to return an iterator pointing to the last element of the container.
To get the last element in an iterator loop you can use std::next() (from C++11). The loop is generally terminated by iterator != container. end() , where end() returns an iterator that points to the past-the-end element.
In this code fragment the user's inten- tion is clear: transmogrify each of the elements in values and insert them at the end of results. Since it is illegal, however, to dereference or increment the end iterator of a container and therefore illegal to write using it, such code is always semantically incorrect.
The C++ memory model guarantees that you can always form a pointer to the element after the last element of an array. If there is none, either the system won't let you allocate an object at this location or it would wrap around. Also, note that this is a potential problem for arrays as other containers can use iterator types which deal with the past the end position in some other suitable form: they fully control how the increment operation work.
An end iterator (at least figuratively) points just past the end of the container. The valid items in the container go from *container.begin()
through *container.end()-1
.
In other words, you can compare some other iterator to the end iterator to see if they'r equal (which tells you that you've reached the end of the items in the container), but you can not dereference that end iterator (i.e., you must not attempt to access an item that it refers to).
Edit: Sorry, kind of misunderstood the question: well, if the container actually used the last byte of memory (rare/unlikely, but theoretically possible) you'd typically see the address wrap around to the beginning of memory, assuming it was an iterator that really worked in terms of address, of course. In such a case, you'd typically see it turn into a 0
address, which can still be distinguished from any valid address (i.e., 0
will convert to a null pointer, which can't be a valid pointer).
In a typical case, however, it's likely that such a thing just wouldn't be allowed to happen. Just for example, on most 32-bit systems, the user is restricted to using the first 2 or 3 gigabytes of address space, and the upper addresses are reserved for the operating system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With