0% found this document useful (0 votes)
25 views

Dynamic Memory Allocation Using New and Delete Operators

The new operator is used to dynamically allocate memory for objects and arrays at runtime. It allocates memory from the free store (heap) and returns the address of the allocated memory. This allows programs to request memory during runtime instead of compile time. The delete operator is used to free up the memory allocated by new to avoid memory leaks. Self-referencing classes have pointers to their own class type, like nodes in linked lists that point to the next node.

Uploaded by

AJispsycho YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Dynamic Memory Allocation Using New and Delete Operators

The new operator is used to dynamically allocate memory for objects and arrays at runtime. It allocates memory from the free store (heap) and returns the address of the allocated memory. This allows programs to request memory during runtime instead of compile time. The delete operator is used to free up the memory allocated by new to avoid memory leaks. Self-referencing classes have pointers to their own class type, like nodes in linked lists that point to the next node.

Uploaded by

AJispsycho YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Dynamic memory allocation

using new and delete operators


Memory Management: new and
delete
• The statement
int arr1[100];
reserves memory for 100 integers
• We must know at the time we write the
program how big the array will be.
• The following approach won’t work:
cin >> size; // get size from user

int arr[size]; // error; array size must be a constant


The new Operator
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char* str = “This is a string”;
int len = strlen(str);
char* ptr;
ptr = new char[len+1];
strcpy(ptr, str);
cout << “ptr=” << ptr << endl;
delete[] ptr;
return 0;
}
Pointer to object
• With the help of new operator we can allocate the memory
to objects.

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();
};

Link List is one example of self reference class


#include<iostream> void display()
using namespace std; {
struct node node *p=start;
{ while(p!=NULL)
{
int data;
cout<<p->data;
node *next; p=p->next;
}; }
class list }
{ }; //End of class
node *start;
public:
list(){start=NULL;} int main()
void additem(int d) {
list ob;
{
ob.additem(10);
node *p=new node; ob.additem(20);
p->data=d; ob.additem(30);
p->next=start;
start=p; ob.display();
return 0;
} }

You might also like