Professional C__ - Marc Gregoire [367]
Simple* mySimpleArray = new Simple[4];
// Use mySimpleArray . . .
delete [] mySimpleArray;
mySimpleArray = nullptr;
Code snippet from ArrayDelete\ArrayDelete.cpp
Always use delete on anything allocated with new, and always use delete[] on anything allocated with new[].
Of course, the destructors are only called if the elements of the array are objects. If you have an array of pointers, you will still need to delete each object pointed to individually just as you allocated each object individually, as shown in the following code:
size_t arrSize = 4;
Simple** mySimplePtrArray = new Simple*[arrSize];
// Allocate an object for each pointer.
for (size_t i = 0; i < arrSize; i++) {
mySimplePtrArray[i] = new Simple();
}
// Use mySimplePtrArray . . .
// Delete each allocated object.
for (size_t i = 0; i < arrSize; i++) {
delete mySimplePtrArray[i];
}
// Delete the array itself.
delete [] mySimplePtrArray;
mySimplePtrArray = nullptr;
Code snippet from ArrayDelete\ArrayDelete.cpp
Instead of storing plain old dumb pointers in your data structures like the arrays above, it is recommended to store smart pointers in your data structures. These smart pointers will automatically deallocate memory associated with them. Smart pointers are discussed in detail later in this chapter.
Multi-Dimensional Arrays
Multi-dimensional arrays extend the notion of indexed values to use multiple indices. For example, a Tic-Tac-Toe game might use a two-dimensional array to represent a three-by-three grid. The following example shows such an array declared on the stack and accessed with some test code:
char board[3][3];
// Test code
board[0][0] = 'X'; // X puts marker in position (0,0).
board[2][1] = 'O'; // O puts marker in position (2,1).
Code snippet from tictactoe\tictactoe.cpp
You may be wondering whether the first subscript in a two-dimensional array is the x-coordinate or the y-coordinate. The truth is that it doesn’t really matter, as long as you are consistent. A four-by-seven grid could be declared as char board[4][7] or char board[7][4]. For most applications, it is easiest to think of the first subscript as the x-axis and the second as the y-axis.
Multi-Dimensional Stack Arrays
In memory, a stack-based two-dimensional array looks like Figure 21-8. Since memory doesn’t have two axes (addresses are merely sequential), the computer represents a two dimensional array just like a one-dimensional array. The difference is the size of the array and the method used to access it.
FIGURE 21-8
The size of a multi-dimensional array is all of its dimensions multiplied together, then multiplied by the size of a single element in the array. In Figure 21-8, the three-by-three board is 3×3×1 = 9 bytes, assuming that a character is 1 byte. For a four-by-seven board of characters, the array would be 4×7×1 = 28 bytes.
To access a value in a multi-dimensional array, the computer treats each subscript as accessing another subarray within the multi-dimensional array. For example, in the three-by-three grid, the expression board[0] actually refers to the subarray highlighted in Figure 21-9. When you add a second subscript, such as board[0][2], the computer is able to access the correct element by looking up the second subscript within the subarray, as shown in Figure 21-10.
FIGURE 21-9
FIGURE 21-10
These techniques are extended to N-dimensional arrays, though dimensions higher than three tend to be difficult to conceptualize and are rarely useful in everyday applications.
Multi-Dimensional Heap Arrays
If you need to determine the dimensions of a multi-dimensional array at run time, you can use a heap-based array. Just as a single-dimensional dynamically allocated array is accessed through a pointer, a multi-dimensional dynamically allocated array is also accessed through a pointer. The only difference is that in a two-dimensional