new and delete c++ code example
Example 1: c++ delet from memory
// Delete pointer
int* ptr1 = new int;
delete ptr1;
// Delete array
int* array = new int[10];
delete[] array;
Example 2: declare dynamic array c++
int main()
{
int size;
std::cin >> size;
int *array = new int[size];
delete [] array;
return 0;
}
Example 3: c++ delete dynamically allocated array
int length = 69;
int * numbers = new int[length];
delete[] numbers;
Example 4: new and delete operator in c++
// Pointer initialized with NULL
// Then request memory for the variable
int *p = NULL;
p = new int;
OR
// Combine declaration of pointer
// and their assignment
int *p = new int;