C++ MEMORY MANAGEMENT.pptx/ kbibuvvw veovn nveknev ovne onv

ChetanRaut43 12 views 10 slides Feb 27, 2024
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

ppt


Slide Content

MEMORY MANAGEMENT IN C++ Presented by:- Chetan Raut. Presented To:- Ms. Shalinee Pareek.

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

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.

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.

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. New operator. Delete operator.

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

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 ;

#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

#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

THANK YOU
Tags