how to delete a dynamic array c++ code example

Example 1: c++ delete dynamically allocated array

int length = 69;
int * numbers = new int[length];
delete[] numbers;

Example 2: what is dynamic memory allocation in c++

In the dynamic memory allocation the memory is allocated during run time.
The space which is allocated dynamically usually placed in a program segment which is known as heap.
In this, the compiler does not need to know the size in advance.
In C++, dynamic memory allocation means performing memory allocation manually by programmer.
It is allocated on the heap and the heap is the region of a computer memory which is managed by the programmer using pointers to access the memory.
The programmers can dynamically allocate storage space while the program is running but they cannot create a new variable name.
  
Example:

Example 3: delete dynamic array c++

int* v=new int[10];  // Creating a dynamic array

delete[] v; // delete the dynamic array

v=nullptr; //recommended

Tags:

C Example