Dynamic memory allocation in C++ allows you to allocate memory for objects at runtime, rather than at compile time. It enables you to create objects whose size or lifetime is determined dynamically during program execution. This is done using the new operator to allocate memory on the heap and the delete operator to deallocate it. Let's explore the concept of dynamic memory allocation in C++ with simple examples.

Introduction to Dynamic Memory Allocation

In C++, memory can be allocated in two ways: static memory allocation and dynamic memory allocation. Static memory allocation is done at compile time, where the size of objects is fixed and determined during the compilation process. On the other hand, dynamic memory allocation allows you to allocate memory at runtime, which gives you more flexibility and control over the size and lifetime of objects.

Dynamic memory allocation is performed using the new operator, which allocates memory on the heap and returns a pointer to the allocated memory. The allocated memory can be used to create objects of various types.

Allocating Memory with `new`

To allocate memory dynamically, you can use the new operator followed by the type you want to allocate. The new operator returns a pointer to the allocated memory. Here's the general syntax:

C++
type* pointerVariable = new type;

For example, let's allocate memory for an integer dynamically:

C++
int* dynamicInt = new int;

In this example, dynamicInt is a pointer to an integer, and new int allocates memory for an integer on the heap. The allocated memory is of type int*, so dynamicInt is assigned the address of the allocated memory.

Deallocating Memory with `delete`

After you have finished using the dynamically allocated memory, it is essential to free up the memory to prevent memory leaks. This is done using the delete operator. The delete operator frees the memory that was previously allocated using new. Here's the general syntax:

C++
delete pointerVariable;

Using the previous example, to deallocate the dynamically allocated integer:

C++
delete dynamicInt;

In this example, delete dynamicInt frees up the memory allocated for the integer. It is important to note that the pointer variable dynamicInt is not deleted or set to nullptr, but the memory it points to is deallocated.

Dynamic Memory Allocation for Arrays

You can also dynamically allocate memory for arrays using the new operator. The syntax for dynamically allocating an array is similar to allocating a single object but with square brackets specifying the array size. Here's an example:

C++
int* dynamicArray = new int[5];

In this example, dynamicArray is a pointer to an integer array of size 5. The new int[5] allocates memory for an integer array of size 5 on the heap.

To deallocate dynamically allocated arrays, you need to use the delete[] operator. Here's an example:

C++
delete[] dynamicArray;

The delete[] operator frees the memory allocated for the entire array.

Usage of Dynamic Memory Allocation

Dynamic memory allocation is useful when you need to create objects whose size or lifetime is determined dynamically at runtime. It is commonly used when working with data structures that require a flexible size, such as dynamic arrays, linked lists, and trees.

Here's an example that demonstrates dynamic memory allocation for a dynamic array:

C++
#include<iostream>
using namespace std;

int main(){
  int size;
  cout << "Enter the size of the array: ";
  cin >> size;
  
  int* dynamicArray = new int[size];
  
  cout << "Enter elements of the array: ";
  for (int i = 0; i < size; ++i) {
      cin >> dynamicArray[i];
  }
  
  cout << "Array elements: ";
  for (int i = 0; i < size; ++i) {
      cout << dynamicArray[i] << " ";
  }
  
  delete[] dynamicArray;
  
  return 0;
}

In this example, the user is prompted to enter the size of the array. Then, memory is dynamically allocated for the array using new int[size]. The user enters the elements of the array, which are stored in the dynamically allocated memory. Finally, the elements are displayed, and the memory is deallocated using delete[] dynamicArray.

Summary

Dynamic memory allocation in C++ allows you to allocate memory at runtime, providing flexibility in creating objects with dynamic sizes or lifetimes. It is performed using the new operator, which allocates memory on the heap, and the delete operator, which deallocates the memory. Dynamic memory allocation is commonly used when working with data structures that require flexible sizes or lifetimes. However, it is crucial to deallocate the dynamically allocated memory using the delete or delete[] to prevent memory leaks.