memory allocation in 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: c++ delete dynamically allocated array

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

Example 3: dynamic memory allocation in c++

char* pvalue  = NULL;         // Pointer initialized with null
pvalue  = new char[20];       // Request memory for the variable

Example 4: dynamic memory allocation in c++

#include <iostream>
using namespace std;

int main () {
   double* pvalue  = NULL; // Pointer initialized with null
   pvalue  = new double;   // Request memory for the variable
 
   *pvalue = 29494.99;     // Store value at allocated address
   cout << "Value of pvalue : " << *pvalue << endl;

   delete pvalue;         // free up the memory.

   return 0;
}

Tags:

Cpp Example