Online Book Reader

Home Category

Professional C__ - Marc Gregoire [368]

By Root 1292 0
array, you need to start with a pointer-to-a-pointer; and in an N-dimensional array, you need N levels of pointers. At first, it might seem like the correct way to declare and allocate a dynamically allocated multi-dimensional array is as follows:

char** board = new char[i][j]; // BUG! Doesn't compile

This code doesn’t compile because heap-based arrays don’t work like stack-based arrays. Their memory layout isn’t contiguous, so allocating enough memory for a stack-based multi-dimensional array is incorrect. Instead, you can start by allocating a single contiguous array for the first subscript dimension of a heap-based array. Each element of that array is actually a pointer to another array that stores the elements for the second subscript dimension. This layout for a two-by-two dynamically allocated board is shown in Figure 21-11.

FIGURE 21-11

Unfortunately, the compiler doesn’t allocate memory for the subarrays on your behalf. You can allocate the first dimension array just like a single-dimensional heap-based array, but the individual subarrays must be explicitly allocated. The following function properly allocates memory for a two-dimensional array:

char** allocateCharacterBoard(size_t xDimension, size_t yDimension)

{

char** myArray = new char*[xDimension]; // Allocate first dimension

for (size_t i = 0; i < xDimension; i++) {

myArray[i] = new char[yDimension]; // Allocate ith subarray

}

return myArray;

}

Code snippet from CharacterBoard\CharacterBoard.cpp

When you wish to release the memory associated with a multi-dimensional heap-based array, the array delete[] syntax will not clean up the subarrays on your behalf. Your code to release an array should mirror the code to allocate it, as in the following function:

void releaseCharacterBoard(char** myArray, size_t xDimension)

{

for (size_t i = 0; i < xDimension; i++) {

delete [] myArray[i]; // Delete ith subarray

}

delete [] myArray; // Delete first dimension

}

Code snippet from CharacterBoard\CharacterBoard.cpp

Now that you know all the details to work with arrays, it is recommended to avoid old C-style arrays as much as possible because they do not provide any memory safety. Instead, use the C++ STL containers like std::array, std::vector, std::list, and so on. For example, use vector for a one dimensional dynamic array. Use vector> for a two dimensional dynamic array and so on.

Working with Pointers

Pointers get their bad reputation from the relative ease with which you can abuse them. Because a pointer is just a memory address, you could theoretically change that address manually, even doing something as scary as the following line of code:

char* scaryPointer = (char*)7;

The previous line builds a pointer to the memory address 7, which is likely to be random garbage or memory that is used elsewhere in the application. If you start to use areas of memory that weren’t set aside on your behalf with new, eventually you will corrupt the memory associated with an object, or the memory involved with the management of the heap, and your program will malfunction. Such a malfunction can manifest itself in several ways. For example, it can manifest itself as invalid results because the data has been corrupted, or as hardware exceptions being triggered due to accessing non-existent memory or attempting to write to protected memory. If you are lucky, you will get one of the serious errors that usually results in program termination by the operating system or the C++ run-time library; if you are unlucky, you will just get a wrong result.

A Mental Model for Pointers

There are two ways to think about pointers. More mathematically minded readers might view pointers simply as addresses. This view makes pointer arithmetic, covered later in this chapter, a bit easier to understand. Pointers aren’t mysterious pathways through memory; they are simply numbers that happen to correspond to a location in memory. Figure 21-12 illustrates a two-by-two grid in the address-based view of the world.

FIGURE 21-12

The addresses in Figure 21-12

Return Main Page Previous Page Next Page

®Online Book Reader