Online Book Reader

Home Category

Professional C__ - Marc Gregoire [366]

By Root 1456 0
where each piece is large enough to hold a single element of the array. For example, a local array of five ints would be declared on the stack as follows:

int myArray[5];

Figure 21-5 shows the state of memory after the array is declared. Declaring arrays on the heap is no different, except that you use a pointer to refer to the location of the array. The following code allocates memory for an array of five ints and stores a pointer to the memory in a variable called myArrayPtr.

FIGURE 21-5

int* myArrayPtr = new int[5];

As Figure 21-6 illustrates, the heap-based array is similar to the stack-based array, but in a different location. The myArrayPtr variable points to the 0th element of the array. The advantage of putting an array on the heap is that you can use dynamic memory to define its size at run time. For example, the following function receives a desired number of documents from a hypothetical function named askUserForNumberOfDocuments() and uses that result to create an array of Document objects.

FIGURE 21-6

Document* createDocArray()

{

int numDocs = askUserForNumberOfDocuments();

Document* docArray = new Document[numDocs];

return docArray;

}

Some compilers allow variable-sized arrays on the stack. This is not a standard feature of C++, so we recommend cautiously backing away when you see it.

In the preceding function, docArray is a dynamically allocated array. Do not get this confused with a dynamic array. The array itself is not dynamic because its size does not change once it is allocated. Dynamic memory lets you specify the size of an allocated block at run time, but it does not automatically adjust its size to accommodate the data. There are data structures that do dynamically adjust in size to their data, such as the STL built-in vector class. It is recommended to use these STL containers like vector instead of standard arrays because they are much safer to use.

There is a function in C++ called realloc(), which is a holdover from the C language. Don’t use it! In C, realloc() is used to effectively change the size of an array by allocating a new block of memory of the new size and moving all of the old data to the new location. This approach is extremely dangerous in C++ because user-defined objects will not respond well to bitwise copying.

Do not use realloc() in C++. It is not your friend.

Arrays of Objects

Arrays of objects are no different than arrays of simple types. When you use new[N] to allocate an array of N objects, enough space is allocated for N contiguous blocks where each block is large enough for a single object. Using new, the zero-argument constructor for each of the objects will automatically be called. In this way, allocating an array of objects using new[] will return a pointer to an array of fully formed and initialized objects.

For example, consider the following class:

class Simple

{

public:

Simple() { cout << "Simple constructor called!" << endl; }

virtual ~Simple() { cout << "Simple destructor called!" << endl; }

};

Code snippet from ArrayDelete\ArrayDelete.cpp

If you were to allocate an array of four Simple objects, the Simple constructor would be called four times.

Simple* mySimpleArray = new Simple[4];

Code snippet from ArrayDelete\ArrayDelete.cpp

The output of this code is:

Simple constructor called!

Simple constructor called!

Simple constructor called!

Simple constructor called!

The memory diagram for this array is shown in Figure 21-7. As you can see, it is no different than an array of basic types.

FIGURE 21-7

Deleting Arrays

When you allocate memory with the array version of new (new[]), you must release it with the array version of delete (delete[]). This version will automatically destruct the objects in the array in addition to releasing the memory associated with them. If you do not use the array version of delete, your program may behave in odd ways. In some compilers, only the destructor for the 0th element of the array will be called because the compiler only knows that you are deleting a pointer to an object, and

Return Main Page Previous Page Next Page

®Online Book Reader