The New And Delete Expressions

Introduction

The expressions new and delete are used to dynamically allocate and free single objects.

When to use new

  • When you wish an object to remain in existence until you delete it.
  • If you want to return a pointer to your object from a function.

Initializing Dynamically Allocated Objects

Example

When we define a variable, we specify a type and a name. When we dynamically allocate an object, we specify a type but do not name the object. Instead, the new expression returns a pointer to the newly allocated object; we use that pointer to access the object:

int i;              // named, uninitialized int variable
int *pi = new int;  // pi points to dynamically allocated,
                         // unnamed, uninitialized int

Destroying Dynamically Allocated Objects

When our use of the object is complete, we must explicitly return the object's memory to the free store. We do so by applying the delete expression to a pointer that addresses the object we want to release.

delete pi;

frees the memory associated with the int object addressed by pi. It is illegal to apply delete to a pointer that addresses memory that was not allocated by new.

Tips on using new/delete

  • Every time you use the new keyword, you should type delete afterwards.
Test *test = new Test();
delete test; //TODO Move this to the right place

Examples

Example 1: Demonstrating overloaded new/delete

#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
 
class MyClass {
  int x, y, z; 
public:
  MyClass() {
    x = y = z = 0;
    cout << "Constructing 0, 0, 0\n";
  }
  MyClass(int i, int j, int k) {
    x = i; 
    y = j; 
    z = k;
    cout << "Constructing " << i << ", ";
    cout << j << ", " << k;
    cout << '\n';
  }
  ~MyClass( ) { 
     cout << "Destructing\n"; 
  }
  void *operator new(size_t size);
  void *operator new[](size_t size);
  void operator delete(void *p);
  void operator delete[](void *p);
  void show() ;
};
 
void *MyClass::operator new(size_t size)
{
  void *p;
 
  cout << "Allocating MyClass object.\n";
  p = malloc(size);
 
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
 
void *MyClass::operator new[](size_t size)
{
  void *p;
 
  cout << "Allocating array of MyClass objects.\n";
 
  p = malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
 
void MyClass::operator delete(void *p)
{
  cout << "Deleting MyClass object.\n";
  free(p);
}
 
void MyClass::operator delete[](void *p)
{
  cout << "Deleting array of MyClass objects.\n";
  free(p);
}
 
void MyClass::show()
{
  cout << x << ", ";
  cout << y << ", ";
  cout << z << endl;
}
 
int main()
{
  MyClass *objectPointer1, *objectPointer2;
 
  try {
    objectPointer1 = new MyClass[3];       // allocate array
    objectPointer2 = new MyClass(5, 6, 7); // allocate object
  } catch (bad_alloc ba) {
    cout << "Allocation error.\n";
    return 1;
  }
 
  objectPointer1[1].show();
  objectPointer2->show();
 
  delete [] objectPointer1; // delete array
  delete objectPointer2;    // delete object
 
  return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.