Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

Aliasing Is Particularly Troublesome With Vector Elements

May 17, 2013

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:

  1. A programmer creates an alias (i.e., a reference or a pointer) to an object.
  2. The object gets deallocated.
  3. 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.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Dr. Dobb's TV