Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL iterators: container.end()

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?

like image 686
Kolyunya Avatar asked Dec 15 '12 22:12

Kolyunya


People also ask

What is end () iterator?

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.

What should begin () and end () do for a 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.

How do you get to the end of an iterator?

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.

Can you dereference end iterator?

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.


2 Answers

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.

like image 131
Dietmar Kühl Avatar answered Oct 07 '22 03:10

Dietmar Kühl


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.

like image 27
Jerry Coffin Avatar answered Oct 07 '22 04:10

Jerry Coffin