Dynamic Memory Allocation in Classes

If you haven't seen the basics of dynamic memory allocation before, then see this page:

Recap of DMA Basics


Dynamic Allocation of Objects

Notation: dot-operator vs. arrow-operator


Using dynamic allocation inside classes

A motivating example

Suppose we want an array as member data of a class, but we don't want to be stuck with a fixed upper bound on the size. How do we accomplish this?

The solution would be to use dynamic allocation on the array. But how to arrange it? Can a dynamic array be physically embedded inside a class? What if an object of that class type is created statically? Then the compiler has to know the size. But the internal contents are to be dynamic?!

Solution: Only the pointer will be in the member data section. Dynamic memory will not be physically in the object, but only linked by the member data pointer.

Setting it up, using good design principles!

Application Example: Dynamically resizing an array

(This was discussed in the pre-requisite course. See COP 3014 notes for full details. Just a summary is listed here)

To change the size of a dynamically allocated array (perhaps to add space), you cannot just append the next consecutive slots. Must find free space for entire array, in one consecutive set of memory. Summary of the basic process:

  1. Dynamically create a new array of desired size. (This step will require a second pointer for temporary use).
  2. Copy the old array's contents into the new one
  3. Deallocate the memory of the old array (avoid memory leak)
  4. Adjust pointers so that the new array has the desired name
This process is used in the following code example.

Code Example: PhoneBook Database Simulation

The above link is to an example that involves two classes and uses dynamic memory allocation.  The classes are Entry and Directory.

Entry -- An object of this class type represents a single entry in a phone book.  The data members stored in an entry object are name, address, and phone number.  Strings (i.e. null-terminated character arrays) are used to store these items.

Directory -- An object of type Directory stores a list of Entry objects, using a dynamic array.  The Directory class also provides services (public member functions) for adding new entries, deleting entries, modifying entries, searching for entries, and displaying all entries in the phone book.  The Directory class also has a function for dynamically resizing the array of Entries when more memory space is needed.
 

Note that in this class, the destructor is also implemented for the Directory class, with a needed definition inside. Since the member data of an object of type Directory includes a pointer, which is being used to point to dynamically allocated space (i.e. the array of entries), it is our job in the code to deallocate that space. When the object is deallocated, the compiler only automatically gives up the space inside the object. The pointer entryList is pointing to data that is physically outside the object, so it doesn't get automatically "cleaned up". But, we can clean up this space (before the object goes away) by doing it in the last function that runs for an object (which is always the destructor). Note that the definition of this destructor is:

 delete [] entryList;
This simply deallocates the dynamic array attached to entryList, before we let this pointer be deallocated along with the object.

Second version of Phone Book example

This one contains overloads of operator<< and operator>> in class Entry, instead of Show() and Load()