Dynamic Memory Allocation Using New and Delete Operators
Dynamic Memory Allocation Using New and Delete Operators
class sample
{
----
----
}
sample *p;
p=new sample;
or
sample *p=new sample;
#include<iostream>
using namespace std; int main()
class sample {
{
int x,y; sample *p;
public: p=new sample;
void getdata(int a,int b)
{ p->getdata(10,20);
x=a;
p->putdata();
y=b;
}
void putdata()
{
delete p;
cout<<x<<y; return 0;
}
};
}
Memory Leak
• A condition caused by a program that does not
free up the extra memory it allocates.
• It occurs when the dynamically allocated memory
is no longer needed but it is not freed.
• If we continuously keep on allocating the memory
without freeing it for reuse, the entire heap
storage will be exhausted.
• In such circumstances, the memory allocation
functions will start failing and program will start
behaving unexpectedly
self referencing class
• If a class has pointer to itself than it is called self
referencing class.
class node
{
int data;
node *next;
public:
void getdata();
void show();
};