20C. Allocating and Deallocating Memory in the Heap

Uninitialized pointer variables

If you do not store something into a pointer variable, then it contains a junk value. The computer does not guess what you want it to point to and does not allocate memory in the heap automatically.


Allocating memory in the heap

To allocate memory for a variable of type T, use expression new T. It allocates the required memory and returns its address (a pointer if type T *). For example,
  int*    pi = new int;
  double* pd = new double;
makes variable pi  point to a newly allocate chunk of memory big enough to hold one int, and makes pd  point to newly allocated memory big enough for a value of type double.


Deallocating memory in the heap

If pointer p was obtained as the value of an expression of the form new T, then statement
  delete p;
deallocates the memory pointed to by p. (It does not destroy variable p itself. It affects the memory pointed to by p.)


Really, delete just recycles the memory. The heap manager (HM) takes ownership of the memory, and it might give that same memory back to you at a future use of new.

Important note. Only delete a pointer that was given to you by new. Never try to delete a local variable of a function. Doing that will corrupt the HM. Chaos ensues. Really!


Watch out: Don't deallocate too soon

Only deallocate memory when you are truly finished with that memory. If you have more than one pointer to a chunk of memory, then deallocating that chunk makes all pointers to it stale; they are pointing to memory that you are no longer allowed to use. Those pointers are called dangling pointers.

A common mistake

After seeing new, some students get the idea that they are required to use it when declaring any pointer variable. That causes them to write
  int* p = new int;
  p = q;
But that allocates memory in the heap (using new), makes p point to that new memory, and then immediately makes p point to a different place, as shown here:

That leads to a memory leak, since the memory allocated using new int has become inaccessible.

If you just want to declare a variable p of type int*, write

  int* p;
If you want to declare p and initialize it to hold the same pointer as q, write
  int* p = q;


Another common mistake

It is easy to forget that statement
  delete p;
does not delete p itself. It deletes the memory to which p points. A common error is to delete a pointer simply because you are done with that pointer. Here is an example, a function that is intended to return a new integer variable in the heap that holds 0.
  int* zerovar()
  {
    int* p = new int;
    *p = 0;
    delete p;
    return p;
  }
That destroys the variable that is being returned. It returns a dangling pointer.

Exercises

  1. Write a statement that creates variable p of type long* and makes p point to newly allocated memory of type long in the heap. Answer

  2. Suppose that the memory pointed to by p from the preceding question is no longer needed. Write a statement that deallocates that memory. Answer

  3. Is the following function a suitable substitute for the new operator for allocating new memory to hold one int?

      int* newInt()
      {
        int n;
        return &n;
      }
    
    That is, can you use
      int* p = newInt();
    
    instead of
      int* p = new int;
    
    Answer