# Pitfalls with vectors

This section presents some of the common pitfalls related to the used of vectors in C++ programs. Thus, it’s important to read this section carefully to avoid misusing vectors.

### Accessing inexisting elements

It’s important to keep in mind that subscripting does not add elements to a vector. Consider the following piece of code.

Vector V1 is empty, i.e. it has no elements to subscript. Thus, it’s not legal in C++ to access elements (or variables) that do not exist in the vector. Bare in mind that the statement V1[0] = -1; cannot be used to add an element (the first one) to the vector. In other words, subcripting such as V1[0] should be used to fectch the value of an existing element, and never to add new elements to the vector. The right way to add an element to the vector is to use push_back, i.e. V1.push_back(-1); adds the first value to the empty vector V1.

Another example.

Vector V2 has six elements, V2[0], V2[1], V2[2], V2[3], V2[4], and V2[5]. There is no element with subscript $6$. Thus, V2[6] is not legal C++, though the program compiles and might even display a value.

Subscripting an element that does not exist in a vector is an error. The compilers are unable to detect this situation. The result of subscripting an element that does not exist in a vector is undefined, i.e. it may lead to run-time errors sometimes, while other times everything may seem to work just fine.

Possible run-time error messages can be “segmentation fault” or “Illegal instruction”. Just try the examples above in, for example, Coliru (click the button “Run code” below the program) and see what you get.

And, one more example.

Imagine $-1$ is not part of the list of values entered by the user. Then, the while-loop (line $16$) will attempt to access an inexisting element, when the subscript variable i gets the value V.size() (recall that the last element of the vector has subscript V.size()-1). A possible way to write the while-loop so that it does not attempt to access elements that do not exist in the vector is given below (though, the loop could also be written in other ways).

// Check whether the user has entered value -1
int i = 0;
while (i < V.size()) {
if (V[i] != -1) {
++i;
}
else {
break; // found -1; exit the loop
}
}


### Initialization

It’s important the keep in minf that the meaning of a statement initializing a vector might depend on wether one uses curly braces or parantheses. See the examples below.

Vector V1 is initialized (line $5$) with $6$ integer elements, all with value $-1$. Note that curly braces have beeen used to define and initialize V1.

Vector V2 is initialized (line $12$) with $2$ integer elements, $6$ and $-1$. Note that parantheses have beeen used to define and initialize V2 (symbol = is optional).

It’s easy to confuse these two ways to define and initialize a vector.

### Modifying vector size in loops

The body of a range-based loop must not modify the size of a vector over which it is iterating. This is not an error that compilers detect.

Consider the example below which traverses an initialized vector V and, for each even element, its double is then added to the end of the vector. Thus, we expect that $4$, $12$, and $16$ are appended to the vector.

The body of the range-based loop, lines $8$ to $12$ above, uses push_back (line $10$) to add new values to the vector. Consequently, the size of the vector is modified inside the loop’s body. Though the program above compiles and may execute, it is not correct and in must cases it does not work correctly. Just try it! (click the button “Run code” or copy the code into your IDE)

One could think that using a for-loop, instead of a range-based loop, would solve the problem, as show next.

The solution above does not work, either. The reason is that the body of the loop, lines $8$ to $12$ above, adds new even elements to the end of the vector (line $10$). Consequently, the size of the vector increases (V.size()) and the newly added even elements will cause new elements to be added, until there’s no more memory available to increase the vector. Thus, the program above is doomed to end with a run time error. Just try it!

Errors may occur when executing a program that contains a for-loop as shown below. This type of loops are, therefore, considered a bad programming practice.

// Don't do this!!
for(int i = 0; i < V.size(); ++i) {
// Do something that modifies the vector V's size
}


The problem can be solved by storing the size of the vector in a variable before entering the loop, line $8$ of the program below, and then use this variable (old_size) in the test condition of the for-loop (line $9$).