Aliasing Is Particularly Troublesome With Vector Elements I would like to continue our discussion with a particularly nasty case in which the result is not well defined.



Last week, I gave an example of how aliasing could make code behave completely differently from what a first look would suggest. This example was well defined, in the sense that executing

*p /= x;

behaves in a way that the C++ language defines, even when x was an alias for *p .

I would like to continue this discussion with a particularly nasty case in which the result is not well defined:

A programmer creates an alias (i.e., a reference or a pointer) to an object. The object gets deallocated. The program tries to use the alias.

Obviously, the effect of such a program is undefined. Less obviously, such a program will often appear to work. The reason is that although the object has been deallocated, its memory may still have the same contents that it had before the deallocation. As a result, when the program uses that deallocated memory, it will appear to see the same data that it would have expected had the memory not been allocated.

You may think that I have just described the dangling reference problem, and indeed dangling references are one example of this phenomenon. However, the problem can come up in another context. This context can be far nastier, both because it is not immediately obvious and because it does not happen every time the program is run.

The context is appending an element to a vector. Recall that a vector has a size, which is the number of elements it currently has, and a capacity, which is the number of elements that will fit in the memory currently allocated for the vector. If a program executes

v.push_back(e);

to append a new element e to a vector v , this code will reallocate the vector if the vector's size before appending the element is equal to its capacity. So, for example, if v is a nonempty vector, and we execute

auto& v_first = &v[0]; v.push_back(e);

then the call to push_back might or might not invalidate v_first , depending on whether v 's size is equal to its capacity before we executed this code.

This behavior of vectors implies that we must be very careful whenever we create a pointer, reference, or iterator that refers to a vector element, because such pointers, references, or iterators will become invalid whenever the vector is reallocated. Moreover, because appending an element to a vector might or might not reallocate that vector, it is hard for testing to reveal errors that might come from invalidating pointers, references, or iterators.

This behavior of vectors has some serious design implications for programs that use vectors, as well as for the design of the vector library itself. We shall look at some of those implications next week.