Before continuing to read, there is a need to recall the following: A subscript is the index in square brackets for the vector name. This index is used to reference the element of the vector. Each index refers to a particular element (value). Index counting begins from 0 and ends at vectorSize – 1. So, if a vector has 10 elements, the first element is at index 0, and the tenth element is at index 9. Using index 10 on the vector to read or change the value of an element at index 10, which does not exist, should output an out-of-range error message. However, with the g++ compiler, as long as a vector has been defined (memory allocated for the elements, even if they are default elements), when an index outside the range is used, the default value of the vector element type or some other value of the type is returned (or is there to be changed).
Out of Range Illustration
Consider the following table:
A | B | C | D | E | F | G | H | I | J | ||||
-2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
The first row shows how ten memory allocations have been made for 10 characters. Below the characters in the table, in the second row, are the correct subscripts (indexes). Using the subscript -1, -2, -3, etc., should result in out-of-range error issued. Using of the subscript 10, 11, 12, etc., should also result in out-of-range error issued.
This article illustrates situations in which out-of-range errors are issued. The compiler used for the code samples in this article is the g++ compiler for Ubuntu. Do not forget to include the vector library into the program, for any compiler.
Out-of-Range Error for Defined Vector
A defined vector is one for which memory has been allocated for the initial elements. The values may be default or practical values for the vector element type. A declared vector without any form of initialization is not a defined vector. For such a vector, there is no memory allocation for any vector element. Such a vector is empty.
G++ Compiler for Ubuntu
Assume that there are ten elements in a vector; a not uncommon mistake made by people who are inexperienced in programming, is to access the tenth element with the subscript of 10. It should be accessed with the subscript of 9, as index counting begins from 0. Consider the following program with 10 elements in the vector, and accessing of the tenth element with the index of 10:
#include <vector>
using namespace std;
int main()
{
vectorvtr = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j'};
char ch = vtr[10]; //error statement
cout<<ch<<endl;
return 0;
}
This program with the g++ compiler for Ubuntu gives an output of ,'', which is a character of nothing (no space). A character of nothing is the default character for characters in C++. There is no eleventh term for this vector. So, the second statement is an error statement that is not detected by the g++ compiler. With the g++ compiler, it is still not detected at runtime, and the program operates wrongly.
Some Other Compilers
When the above program is used with some other compilers, the error statement is detected at runtime, and an exception is thrown with an error message issued at the output. The error is still not detected at compile time, though.
Consider the following declaration:
This may not look like a definition, but it is a definition. There are ten memory locations for the ten vector elements with the default character value.
Out-of-Range Error for Undefined Vector
A declared vector without any form of initialization is not a defined vector. For such a vector, there is no allocation for any vector element. Such a vector is empty.
G++ Compiler for Ubuntu
When a declared vector has no initialization (or has no default values), the use of subscript is not applicable to the vector in accessing any element of the vector as there is non. Even the zero index cannot be used to access the first element, which is not present. In the following program, an attempt is made to access the first element, which is not present:
#include <vector>
using namespace std;
int main()
{
vectorvtr;
char ch = vtr[0]; //error statement
cout<<ch<<endl;
return 0;
}
The first statement in the main function declares a vector without any memory location for any of its elements. This statement is not a definition. The second statement attempts to read the value at index 0. This is an error statement because the vector has no element, and so no element is present at any index zero.
With the g++ compiler, the program compiles successfully, but at runtime, when the second statement in the main function is reached, the program stops and the following error message is issued:
Some Other Compilers
Execution of the above program in other compilers, and noting their corresponding error messages is left as an exercise to the reader.
Conclusion
Vector subscript out-of-range error occurs when an attempt is made to access a vector element using a subscript that is outside the index range. Out-of-range error is not the same as Segmentation fault (core dumped) all the time.