➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv
1. MEMORY MANAGEMENT IN C++
Presented by:- Chetan Raut.
Presented To:- Ms. Shalinee Pareek.
2. What’s in it for you ?
What is memory management ?
Why we need memory management ?
Memory allocation and deallocation in c++
C++ new operator
C++ delete operator
3. What is memory management ?
Memory management can be defined as the process to
manage a computer’s memory.
Example: Assigning memory to programs. Variables etc.
So, that it doesn’t affect the overall
performance.
4. Why we need memory management?
Memory management is required to avoid wastages of
memory and to make sure allocation takes place
efficiently.
During the declaration of arrays, sometimes the exact
memory is not known to us, and that is why we declare an
array of max size. Which results in memory wastages.
To avoid such case, we use memory allocation.
5. Memory allocation and deallocation in C++
Programming languages like java, python, etc. have the
compiler that manages the memory allocation.
In the case of C++, the allocation and deallocation of
memory are done manually.
In C++, there are two operators that is used for the
allocation and deallocation of memory.
1) New operator.
2) Delete operator.
6. C++ New Operator
The new operator in C++ is used for dynamic memory
allocation.
The new operator is responsible for the creation of the object.
Syntax:-
Pointer variable= new datatype;
Syntax for arrays:-
Pointer Variable = new datatype[size];
In case of arrays, the new keyword returns
the address of first element
Example
int *ptr;
ptr = new int;
*ptr= 75;
Keyword Data Type of pointer
7. C++ Delete Operator
The Delete operator in C++ is used for deallocation of memory or
for releasing of memory space.
Once the memory is no longer required, then we have to
deallocation the memory using the delete operator.
Syntax:-
delete Pointer variable;
Syntax for arrays:-
delete[]Pointer Variable;
Example
int *ptr;
ptr = new int;
*ptr= 75;
//After printing
delete ptr;
8. #include<iostream>
Using namespace std;
Int main(){
int *ptr1;
int *ptr2;
int *ptr3;
int avg;
ptr1= new int;
ptr2= new int;
ptr3=new int;
cout<<“Enter the first number: ”;
cin>>*ptr1;
cout<<“Enter the second number: ”;
cin>>*ptr2;
cout<<“Enter the third number: ”;
cin>>*ptr3;
}
avg=(*ptr1+*ptr2+*ptr3)/3;
cout<<“Average is : ”<<avg<<endl;
delete ptr1;
delete ptr2;
delete ptr3;
}
Output:-
Enter the first number: 5
Enter the second number: 5
Enter the third number: 2
Average is : 4
9. #include<iostream>
Using namespace std;
Int main(){
int size;
cout<<“Enter the size: ”;
cin>>size;
int *ptr;
ptr= new int[size];
cout<<“Enter the element: ”<<endl;
for(int i=0;i<size;i++)
{
cout<<“Element: ”;
cin>>ptr[i];
}
{ cout<<“nElements that you have entered : ”<<endl;
for(int i=0;i<size;i++)
{
cout<<“Element : ”<<ptr[i]<<endl;
}
delete ptr;
}
Output:-
Enter the size: 4
Enter the element:
Element: 2
Element: 4
Element: 7
Element: 9
Elements that you have entered :
Element: 2
Element: 4
Element: 7
Element: 9