Dynamic Memory allocation in C++

Getting Data from MySql table in php
Pointers 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.


Getting Data from MySql table in php
Pointers in C++

Leave a Reply