Declaring and Defining Dynamic Arrays

 < Day Day Up > 



A dynamic array is one that has been created in application heap memory using the new[] operator. The benefit of dynamic array creation is that the array size can be determined at runtime and an array meeting your exact storage needs can be created on the fly. In this section I’ll show you how to declare and create dynamic arrays. Once a dynamic array is created its elements are accessed like an ordinary array.

Dynamically Allocated Single Dimensional Arrays

Figure 8-17 shows a memory representation of a dynamic array of three integer pointers.

click to expand
Figure 8-17: Dynamic Array of Three Integer Pointers

Before showing you the code to create the array depicted in figure 8-17 it will help you to know the steps involved with the dynamic array creation process.

You first need to declare a pointer to the type of objects the array will contain. In figure 8-17 the array contains integer pointers so the pointer to the array must be a pointer to a pointer. If, however, you simply wanted an array of integers, you would just need a pointer to an int. For now, let us stick with an array of integer pointers. Here is the declaration for the pointer:

int** int_pointer_array;

The second step is to dynamically allocate the memory space for the array of integer pointers in the heap and assign the address to the pointer. You do this with the new[] operator:

int_pointer_array = new int*[3];

This line allocates memory for an array of three integer pointers in the heap and assigns the address of the first element to the int_pointer_array pointer located in the stack.

Once the array of integer pointers is created you can dynamically create each integer object. You will do this with the new operator as you’ve done before with regular pointers.

int_pointer_array[0] = new int(1);

This line allocates space on the heap for an integer object and assigns its address to the first element of int_pointer_array. Notice how ordinary array subscript notation is used on the dynamic array.

When your program no longer needs the dynamically allocated array you must remember to release the memory resources back to the operating system using the delete and delete[] operators. The following code releases each object pointed to by the elements of int_pointer-array and then deletes the array itself:

for(int i=0; i<3; i++){    delete int_pointer_array[i]; } delete [] int_pointer_array;

Example 8.15 shows a complete example:

Listing 8.15: dynamic array allocation

start example
1  int** int_pointer_array = new int*[3];  2  3  for(int i = 0; i<3; i++)  4      int_pointer_array[i] = new int(i+1);  5  6  for(int i = 0; i<3; i++)  7      cout<<*int_pointer_array[i]<<endl;  8  9  for(int i = 0; i<3; i++) 10      delete int_pointer_array[i]; //release memory for each object 11 12  delete[] int_pointer_array; //then release the array
end example

Notice on line 1 how the creation of the dynamic array takes place on the same line as its declaration. On line 7 each integer value is accessed using ordinary pointer dereferencing.

The integer value 3 on line 1 of example 8.15 can be replaced with an integer variable. The variable’s value can be set at runtime and used to set the size of the array. Example 8.16 prompts the user to enter a value to be used for dynamic array allocation. This example simply declares an array of integers, not an array of integer pointers, although you can dynamically create an array of any type.

Listing 8.16: dynamic arrayallocation

start example
1  int array_size = 0;  2  int* int_array = NULL;  3  4  cout<<"Please enter array size: ";  5  cin>>array_size;  6  7  int_array = new int[array_size];  8  9  for(int i=0; i<array_size; i++) 10      int_array[i] = i+1; 11 12  for(int i=0; i<array_size; i++) 13  cout<<int_array[i]<<endl; 14 15  delete[] int_array; //release the array when done
end example

Dynamically Allocated Multi-Dimensional Arrays

Dynamically allocated multi-dimensional arrays are not as easy to create as their statically allocated cousins, but once you have created them they’re as easy to use as a normal array. I’ll show you two methods for dynamically creating a multi dimensional array.

The first method involves knowing the dimension of the smallest set of elements and dynamically allocating the largest. This is demonstrated in example 8.17.

Listing 8.17: dynamic multi-dimensional array allocation

start example
1  int   rows = 0;  2  const int cols = 5;  3  int (*two_d_int_array)[cols];  4  5  cout<<"Please enter the number of rows: ";  6  cin>>rows;  7  8  two_d_int_array = new int[rows][cols];  9 10  for(int i=0; i<rows; i++) 11      for(int j=0; j<cols; j++) 12        two_d_int_array[i][j] = j+1; 13 14  for(int i=0; i<rows; i++){ 15      for(int j=0; j<cols; j++){ 16        cout<<two_d_int_array[i][j]; 17      } 18     cout<<endl; 19  } 20  delete[] two_d_int_array; //release the array
end example

Study the syntax on line 3. A pointer named two_d_int_array is declared to point to arrays containing 5 integer objects. What must be done next is to dynamically allocate the number of rows the two-dimensional array will contain. This is accomplished by getting an integer value from the user on lines 5 and 6 and assigning it to the variable rows. Next, the variable rows is used to create the array using familiar syntax on line 8. The rest of the code initializes the newly created array of integers and prints the values to the screen. This method is restrictive in that the values of the smaller dimensions must be known at compile time. Running the code from example 8.17 with a row value of 6 produces the results shown in figure 8-18.

click to expand
Figure 8-18: Results of Running Example 8.17 Using Row Value 6

To dynamically allocate both dimensions you must resort to the fundamental knowledge that a multi-dimensional array is an array of arrays. The following example allows the user to dynamically allocate both the rows and cols of a two-dimensional array.

Listing 8.19: dynamically allocating2-dimensional array

start example
1   int rows = 0;  2  int cols = 0;  3  4  int** two_d_int_array;  5  6  cout<<"Please enter number of rows and columns: ";  7  cin>>rows>>cols;  8  9  two_d_int_array = new int*[rows]; 10 11  for(int i=0; i<rows; i++) 12      two_d_int_array[i] = new int[cols]; 13 14  for(int i=0; i<rows; i++) 15      for(int j=0; j<cols; j++) 15        two_d_int_array[i][j] = j+1; 17 18  for(int i=0; i<rows; i++){ 19      for(int j=0; j<cols; j++){ 20        cout<<two_d_int_array[i][j]; 21     } 22     cout<<endl; 23  } 24  for(int i = 0; i<rows; i++) 25      delete[] two_d_int_array[i]; //release each row array 26 27  delete[] two_d_int_array; //release array of rows
end example

As you study example 8.19 you will notice there is an extra step required when utilizing this method. First, on line 4, a pointer to a pointer to an integer is declared. Lines 6 & 7 get the array dimensions from the user. Line 9 creates an array of integer pointers using the rows variable. The extra step occurs at line 11. Here the for loop allocates an array of integers for each row using the variable cols. The remaining code uses normal array notation to initialize each element of the array and print the values to the screen. Figure 8-19 shows the results of running this code using a rows value of 10 and cols value of 6.

click to expand
Figure 8-19: Results of Running Example 8.19 Using rows = 10 & cols = 6



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net