Thursday, May 13, 2021
Home Dynamic Memory Allocation(DMA) Dynamic Memory allocation in C++

Dynamic Memory allocation in C++

slide-1-728It’s the concept of dynamic memory allocation at the runtime, it is simply the use of “new” and “delete” operators. Normally we cannot specify at the runtime that what will be the size of our array or what will be our memory allocation after the definition of size of array at the runtime? so it’s the solution to that, we can allocate memory for an array by defining its size while the program is running,simply its all about dynamic memory allocation at runtime. We use the “new” and “delete ” operators as:

e.g.    int  number;


                int *ptr = new int [number];

this new operator provides the dynamic memory allocation.

Now if we must have to delete this memory:

                Delete[] ptr;

Here the “delete” operator finishes the memory allocation.

Now in this case “ptr” is an array of the size equal to “number”. It will be clear from the following example:

//dynamic memery allocation in C++
using namespace std;
int main()
float *p;
int i;
p = new float [10]; // get a 10-element array
if(!p) {
cout << "Allocation Failuren";
return 1;
// assign the values 100 through 109
for(i=0; i<10; i++) p[i] = 100.00 + i;

// display the contents of the array
for(i=0; i<10; i++)  cout << p[i] << " ";

delete [] p; // delete the entire array

return 0;

In the above example first we defined a pointer and then we set a memory by using a “new” operator, then we used the pointer as in the method of Arrays, and at the end we deleted the dynamic memory allocation.


- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....