Introduction to Programming and C++

Contents Previous Topic Next Topic

Suppose that we have a dynamically allocated array with 4 entries and we now need an array containing 8 entries where we want to keep the first 4 entries. Because the request for memory came from the operating system, it is impossible to as for 4 more locations. Instead, we must request memory for a new array, copy the values over, delete the memory for the smaller array and then reassign the original pointer. This is shown in Program 1.

Program 1. Dynamically allocated array.

#include <iostream>

using namespace std;

int main() {
	int array_size = 4;

	int * array = new int[array_size];

	for ( int i = 0; i < array_size; ++i ) {
		array[i] = i*i;
	}

	/*******************************************
	 * Start Resizing the Array                *
	 *******************************************/

	// 1. we require a temporary pointer
	int * array_tmp = 0;

	// 2. allocate twice as much memory
	array_tmp = new int[2 * array_size];

	// 3. copy over the old values
	for ( int i = 0; i < array_size; ++i ) {
		array_tmp[i] = array[i];
	}

	// 4. delete the old array
	delete [] array;
	array = 0;

	// 5. assign the temporary pointer to the original
	array = array_tmp;

	// 6. set the temporary pointer to 0
	array_tmp = 0;

	// 7. update the size of the array
	array_size *= 2;

	/*******************************************
	 * Finish Resizing the Array               *
	 *******************************************/

	// continue using the new array...

	for ( int i = array_size/2; i < array_size; ++i ) {
		array[i] = i*i;
	} 

	cout << "The squares are:";

	for ( int i = 0; i < array_size; ++i ) {
		cout << " " << array[i];
	}

	cout << endl;

	// clean up...
	delete [] array;

	return 0;
}

1. Initial State

We have an array array with four entries which are filled. We require a temporary pointer which is initially pointing to null (0). This is demonstrated in Figure 1. The size of the array is stored in the variable array_size.

Figure 1. The initial state of the array.

2. Allocating Twice as Much Memory

The next step is to allocate twice as much memory using the command

	array_tmp = new int[2 * array_size];

The result is shown in Figure 2.

Figure 2. Allocating more memory.

3. Copying the Old Values

We must now copy over all the old values into the array. This can be done with the for loop

	for ( int i = 0; i < array_size; ++i ) {
		array_tmp[i] = array[i];
	}

This is shown graphically in Figure 3.

Figure 3. Copying over the old values.

4. Deleting the Old Memory

Now that all the values have been copied, we no longer need the memory used by the original array and therefore we delete it.

	delete [] array;
	array = 0;

The second line is not necessary, however, we include it to emphasize the memory is now gone. This is shown in Figure 4.

Figure 4. Deleting the old memory.

5. Assigning the Temporary Pointer to Array

The next step is to assign the temporary pointer to array. This is done with the simple assignment

	array = array_tmp;

Now both pointers are pointing to the same memory location, as is shown in Figure 5.

Figure 5. Reassigning array to point to the new array.

6. Set the Temporary Pointer to Point to 0

At this point, we have two pointers pointing to the same array. While not necessary, it is safer if we reset the temporary pointer to 0, as this way, we cannot accidentally access or delete [] the same array using two different variables. Thus we set

	array_tmp = 0;

and the final state is shown in Figure 6.

Figure 6. The final state after setting the temporary pointer to null (0).

7. Updating the Array Size

You will recall that we stored the array size in the variable array_size. We must now double this value to properly track the correct array size. We need only double the value:

	array_size *= 2;

Continue

We have now successfully resized the array. We can continue using the variable array as per normal. In Program 1, we simply continue filling the entries with the square of the index.

Questions

1. In Program 1, we doubled the size of the array. Rewrite Program 1 so that it halves the size of the array. Be sure not to copy over more values than can be stored in the new (smaller) array.


Contents Previous Topic Top Next Topic