dynamic memory allocation in c and c++ code example
Example 1: dynamic memory allocation
int *p = new int; // request memory
*p = 5; // store value
cout << *p << endl; // Output is 5
delete p; // free up the memory
cout << *p << endl; // Output is 0
Example 2: dynamic memory management c++
// declare an int pointer
int* pointVar;
// dynamically allocate memory
// using the new keyword
pointVar = new int;
// assign value to allocated memory
*pointVar = 45;