Lecture 07

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Array Index Out of Bounds

Consider the following declaration:


double num[10];
int i;

The component num[i] is valid, that is, i is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.


The index—say, index—of an array is in bounds if index >= 0 and index <= ARRAY_SIZE - 1.

If either index < 0 or index > ARRAY_SIZE - 1, then we say that the index is out of bounds.
Unfortunately, in C++, there is no guard against out-of-bound indices. Thus, C++ does not check
whether the index value is within range—that is, between 0 and ARRAY_SIZE -1. If the index
goes out of bounds and the program tries to access the component specified by the index, then
whatever memory location is indicated by the index that location is accessed. This situation can
result in altering or accessing the data of a memory location that you never intended to modify or

access.
A loop such as the following can set the index out of bounds:
for (i = 0; i <= 10; i++)
list[i] = 0;
Here, we assume that list is an array of 10 components. When i becomes 10, the loop
test condition i <= 10 evaluates to true and the body of the loop executes, which results
in storing 0 in list[10]. Logically, list[10] does not exist.
On some new compilers, if an array index goes out of bounds in a program, it is possible that the program
terminates with an error message

Array Initialization During Declaration


Like any other simple variable, an array can be initialized while it is being declared. For
example, the following C++ statement declares an array, sales, of five components and
initializes these components.
double sales[5] = {12.25, 32.50, 16.90, 23, 45.68};
The values are placed between curly braces and separated by commas—here,
sales[0] = 12.25, sales[1] = 32.50, sales[2] = 16.90, sales[3] = 23.00, and sales[4] = 45.68.
The previous statement is, therefore, equivalent to:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; When initializing arrays as they are declared, it
is not necessary to specify the size of the array.
Partial Initialization of Arrays During Declaration
When you declare and initialize an array simultaneously, you do not need to initialize all
components of the array. This procedure is called partial initialization of an array
during declaration.
The following examples help to explain what happens when you declare and partially
initialize an array.
The statement: int list[10] = {0};
declares list to be an array of 10 components and initializes all of the components to 0.

The statement: int list[10] = {8, 5, 12};


declares list to be an array of 10 components and initializes list[0] to 8, list[1] to 5, list[2] to 12,
and all other components to 0.

the statement: int list[] = {5, 6, 3};


declares list to be an array of three components and initializes list[0] to 5, list[1] to
6, and list[2] to 3. In contrast, the statement:
When you partially initialize an array, then all of the elements that follow the last
uninitialized elements must be uninitialized. Therefore, the following statement will
result in a syntax error:
int list[10] = {2, 5, 6, , 8}; //illegal
In this initialization, because the fourth element is uninitialized, all elements that follow
the fourth element must be left uninitialized.

Some Restrictions on Array Processing


Consider the following statements:
int myList[5] = {0, 4, 8, 12, 16}; //Line 1
int yourList[5]; //Line 2

Suppose that you want to copy the elements of myList into the corresponding elements of yourList. The following
statement is illegal:
yourList = myList; //illegal

In fact, this statement will generate a syntax error. C++ does not allow aggregate operations on
an array. An aggregate operation on an array is any operation that manipulates the entire array as
a single unit.
To copy one array into another array, you must copy it component-wise—that is, one
component at a time. This can be done using a loop, such as the following:
for (int index = 0; index < 5; index ++)
yourList[index] = myList[index];

Note: cin >> yourList; //illegal To read data into yourList, you must read one component at a time, using a loop

Similarly, determining whether two arrays have the same elements and printing the
contents of an array must be done component-wise. Note that the following statements
are illegal in the sense that they do not generate a syntax error; however, they do not
give
the desired results.
cout << yourList;
if (myList <= yourList)
.
Arrays as Parameters to Functions
Now that you have seen how to work with arrays, a question naturally arises: How are arrays passed as
parameters to functions?
By reference only: In C++, arrays are passed by reference only.
Because arrays are passed by reference only, you do not use the symbol & when declaring an array as a
formal parameter.
When declaring a one-dimensional array as a formal parameter, the size of the array is
usually omitted. If you specify the size of a one-dimensional array when it is declared as
a formal parameter, the size is ignored by the compiler.
Example : in this example we write a simple program to pass array as a parameter to function and print the array
using the function.

You might also like