Dynamic Memory Allocation In C++
Dynamic Memory Allocation In C++
What is dynamic memory allocation?
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get memory allocated on Stack
Why we use Dynamic Memory Allocation?
**I AM KEEP IMPORVING THIS ARTICLE**
#include<iostream>
using namespace std;
class simple
{
int *p; // defining a pointer as we are going to work with dma
public:
simple(int x) // Parameterized constructor
{
// allocating dynamic memory
cout<<"I am paramertized constructor"<<endl;
p=new int ; //new int will return the dynamically created adress
*p = x; // x value is stored in *p i.e. value stored in p.
}
simple(simple &obj) // USER DEFINED COPY CONSTRUCTOR
{
cout<<"I am copy constructor"<<endl;
// now copying the values of object 1 variable to object 2 variable.
p=new int()
*p=*obj.p
}
void disp() // for diplaying values
{
cout<<"The value is : "<<*p<<endl;
cout<<"I will give the adress of varaible : "<<p<<endl;
// we will se that for each value address is different
}
};
int main()
{
simple s1(10);
simple s2(s1);
s1.disp();
s2.disp();
return 0;
Comments
Post a Comment