Here in this page we will perform dynamic memory allocation in C++ for these two Following,
Dynamic memory allocation in array in C++
The array variable may be a good example to understand the dynamic memory allocation. As we know that array is fixed size. That is, when we declare an array variable, we also give it a size in which we can not change at run-time and so it can only store values up to the given size or less.
Let’s make it easy,
You can understand this as if we have created a program and stored a record of 5 students using an array variable (by declaring array size 5). It can store only 5 student records and if we store less than 5 student records in it, then the remaining memory will be waste which will remain in the program even if not needed.
Meaning if you store fewer records, then memory will be waste and even if needed, we cannot store more than 5 student records in it.
In such a situation we have to change the array size in the program to store more records, which will not be considered the right way.
But in the dynamic memory allocation, we can increase and decrease the size of the array according to our requirement at run-time. Thus, there will not be any kind of memory waste here.
But remember that the run-time allocates memory is required to release from the delete operator.
syntax
To allocate memory,
data-type pointer-variable = new data-type[size];
here is the Program,
#include<iostream>
using namespace std;
int main()
{
int i,size;
int *ptr;
clrscr();
cout<<"Enter Array Size: ";
cin>>size;
ptr=new int[size];
cout<<"\nEnter "<<size<<" Element in Array: ";
for(i=0;i<size;i++)
{
cin>>*(ptr+i); // here ptr[i]; will also work
}
cout<<"Values Are\n";
for(i=0;i<size;i++)
{
cout<<ptr[i]<<"\t"; // (ptr+i); will also work
}
delete[] ptr; // delocating memory located by new operator
return 0;
}
OUTPUT
1st Execution, 3 elements are stored and the array variable will allocate memory for only 3 elements.
Enter Array Size: 3
Enter 3 Element in Array: 1 2 3
Values Are
1 2 3
2nd Execution, In 2nd execution, 6 elements are stored and the array variable will allocate memory for only 6 elements.
Enter Array Size: 6
Enter 6 Element in Array: 1 2 3 4 5 6
Values Are
1 2 3 4 5 6
Explanation
In this way, we can see how dynamic memory allocation is important.
Dynamic Object for structure and class in C++
Yes, it is possible to create a variable in structure and class and its useful when we work with array and object.
Because, class and structure are almost the same, hence dynamic memory allocation in both is done in the same way.
syntax
structure-name pointer-variable = new class-name
structure-name pointer-variable = new structure-name;
For more read this similarities and difference between structure and class.
Here is the program, where we used to structure
#include <stdio>
#include<iostream>
using namespace std;
struct test
{
int x,y,sum;
void get_sum(int,int);
};
void test::get_sum(int a, int b)
{
x = a;
y = b;
sum = x+y;
cout<<x<<" + "<<y<<" = "<<sum;
}
int main()
{
test *ptr = new test; // dynamic object created
ptr->get_sum(3,4); // member function calling
delete ptr; // releasing memory using delete operator
return 0;
}
OUTPUT
3 + 4 = 7
Previous- memory allocation in C++
Next- storage specifiers in C++