Introduction to new/delete

Dynamic Memory Allocation in C++

Comparing C and C++ memory allocating: Example 1: Simple allocation of built-in type:
void f1(void)
{
    // Dynamically allocate space for an int
  int *i1 = (int *) malloc(sizeof(int)); // C and C++ (4 bytes)
  int *i2 = new int;                     // C++ only (4 bytes)

    // Use i1 and i2

    // Release the memory (programmer)
  free(i1);   // C and C++
  delete i2; // C++ only
}


Example 2: Allocation of a struct:

  // On-screen graphic
struct Sprite
{
  double x;
  double y;
  int weight; 
  int level;
  char name[20];
};    

void f2(void)
{
  Sprite s1; // Allocated on the stack (handled by compiler)

    // Dynamically allocate on the heap (handled by the programmer)
  Sprite *s2 = (Sprite *) malloc(sizeof(Sprite)); // 44 bytes, ignoring alignment (C and C++)
  Sprite *s3 = new Sprite;                        // 44 bytes, ignoring alignment (C++ only)

  s1.level = 1;  // s1 is a Sprite struct
  s2->level = 2; // s2 is a pointer to a Sprite struct
  s3->level = 3; // s3 is a pointer to a Sprite struct

    // Other stuff ...

    // Release the memory (programmer)
  free(s2);   // C and C++
  delete s3; // C++ only

} // s1 goes out of scope and the memory is released automatically


Example 3: Allocating arrays of built-in types:

void f3(void)
{
    // Allocate space for array of 10 chars and 10 ints (C and C++)
  char *p1 = (char *) malloc(10 * sizeof(char)); // 10 bytes
  int *p2 = (int *) malloc(10 * sizeof(int));    // 40 bytes

    // Allocate space for array of 10 chars and 10 ints (C++ only)
  char *p3 = new char[10]; // 10 bytes
  int *p4 = new int[10];   // 40 bytes

    // Use p1, p2, p3, p4 ...

    // Release the memory (programmer)
  free(p1);      // C and C++
  free(p2);      // C and C++
  delete [] p3; // C++ only (array delete)
  delete [] p4; // C++ only (array delete)
}

Note: If you allocate an array with new (using the array operator, []), you MUST use delete with the array operator as well. Failure to do so causes your program to be undefined (and lose points on an assignment) EVEN IF YOU GET THE CORRECT OUTPUT.


Example 4: Allocating arrays of structs (user-defined type):

  // On-screen graphic
struct Sprite
{
  double x;
  double y;
  int weight; 
  int level;
  char name[20];
};    
	
void f4(void)
{
    // Allocated array of 10 Sprites on the stack (handled by compiler)
  Sprite s1[10]; 

    // Dynamically allocate array of 10 Sprites on the heap (handled by the programmer)
  Sprite *s2 = (Sprite *) malloc(10 * sizeof(Sprite)); // 440 bytes, ignoring alignment (C and C++)
  Sprite *s3 = new Sprite[10];                         // 440 bytes, ignoring alignment (C++ only)

  s1[0].level = 1; // s1[0] is a Sprite struct
  s2[0].level = 2; // s2[0] is a Sprite struct
  s3[0].level = 3; // s3[0] is a Sprite struct

  s1->level = 3; // Does this work?
  s2->level = 4; // Does this work?
  s3->level = 5; // Does this work?

    // Other stuff ...

    // Release the memory (programmer)
  free(s2);      // C and C++
  delete [] s3; // C++ only (array delete)

} // s1 goes out of scope and the memory is released automatically


Example 5: Modified Sprite struct:

  // On-screen graphic, new version
struct Sprite2
{
  double x;
  double y;
  int weight; 
  int level;
  char *name; // not an array
};    

void f5(void)
{
    // Dynamically allocate Sprite2 on the heap (handled by the programmer)
  Sprite2 *s1 = (Sprite2 *) malloc(sizeof(Sprite)); // 28/32 bytes, ignoring alignment (C and C++)
  Sprite2 *s2 = new Sprite2;                        // 28/32 bytes, ignoring alignment (C++ only)

    // Dynamically allocate 10 chars on the heap (programmer)
  s1->name = (char *) malloc(10 * sizeof(char)); // 10 bytes (C and C++)
  s2->name = new char[10];                       // 10 bytes (C only)

    // Use s1 and s2 ...

    // Release memory for chars
  free(s1->name);     // C and C++
  delete [] s2->name; // C++ only (array delete)
  
    // Release memory for Sprite2
  free(s1);   // C and C++
  delete s2; // C++ only
}


Example 6: Stack and heap allocations:

void f6(void)
{
    // Allocate 10 characters on the stack
  char a[10]; 

    // Point at the first element (C and C++)
  char *p1 = a;

    // Allocate space for array of 10 chars and 10 ints (C and C++)
  char *p2 = (char *) malloc(10 * sizeof(char)); // 10 bytes

    // Allocate space for array of 10 chars and 10 ints (C++ only)
  char *p3 = new char[10]; // 10 bytes

    // Use p1, p2, p3
    // All three pointers work in the exact same way
    // There is no way to tell how the memory was allocated

    // Release the memory
  free(p2);      // C and C++
  delete [] p3; // C++ only

} // a is automatically released here
  // calling free or delete on a is very dangerous!