SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Lecture 19
MEMORY MANAGEMENT
 Dynamic and static memory allocation
 New operator
 Delete operator
Presented by:
 Memory management is the process of controlling
and coordinating, computer memory assigning
portions called blocks to various running programs
to optimize overall system performance.
 Memory management resides in hardware, in the
OS (operating system), and in programs and
applications.
MEMORY MANAGEMENT
 Memory is allocated for the declared variable by the
compiler. The memory is allocated during compile
time. Since most of the declared variables have
static memory, this kind of assigning the address of
a variable to a pointer is known as static memory
allocation. The lifetime of a variable in static
memory is the lifetime of the program.
STATIC MEMORY ALLOCATION
 Allocation of memory at the time of execution (run
time) is known as dynamic memory allocation.
Dynamic allocation is achieved using certain
functions like malloc(), calloc(), realloc(), free in C
and operators like "new", "delete" in C++. Dynamic
allocation of memory space is done by using these
functions when value is returned by functions and
assigned to pointer variables.
DYNAMIC MEMORY ALLOCATION
 The new operator denotes a request for memory
allocation at run time. If sufficient memory is
available, new operator initializes the memory and
returns the address of the newly allocated and
initialized memory to the pointer variable.
 Syntax to use new operator:
pointer-variable = new data-type;
int *p;
p = new int;
NEW OPERATOR
 Initialize memory:We can initialize the memory using
new operator.
pointer-variable = new data-type(value);
Example:
int *p = new int(25);
float *p = new float(75.25);
Char *p=new char(‘A’);
NEW OPERATOR (CONTINUE)
 Combine declaration of pointer and their assignment
int *p = new int;
Example:
int *p = new int;
float *p = new float;
NEW OPERATOR (CONTINUE)
 Allocate block of memory: new operator is also used to
allocate a block(an array) of memory of type data-
type.
pointer-variable = new data-type[size];
int *p = new int[10];
 Dynamically allocates memory for 10 integers continuously of
type int and returns pointer to the first element of the
sequence, which is assigned to p(a pointer). p[0] refers to first
element,p[1] refers to second element and so on.
NEW OPERATOR (CONTINUE)
 When memory allocated by new operator is no longer
required, it is freed using operator delete.
Syntax:
delete pointer-variable;
Here, pointer-variable is the pointer that points to the
data object created by new.
Examples:
delete p;
delete q;
DELETE OPERATOR
 To free the dynamically allocated array pointed by
pointer-variable, use following form of delete:
delete[] pointer-variable;
Example:
delete[] p;
The brackets [] indicates the array has been deleted. If
you need to delete a single object then, you don't need
to use brackets.
DELETE OPERATOR (CONTINUE)
#include <iostream>
using namespace std;
class Box {
public:
Box() {
cout <<“box"<<endl; }
};
int main( ) {
Box* myBoxArray = new Box[4]; // Box memory
allocation
delete[] myBoxArray; // Box memory is released or
delete
return 0;
}
OUTPUT:
box
box
box
box
#include <iostream>
using namespace std;
int main ()
{
int i,n,total=0;
int * p;
cout << "How many subjects would you like to type? ";
cin >> i;
p= new int[i];
for (n=0; n<i; n++)
{
cout <<endl<<"Enter marks in subject "<<1+n<<":";
cin >>*(p+n); or cin>>p[i];
}
for (n=0; n<i; n++){
total=total+*(p+n); or total=total+p[i];
}
cout<<endl<<"Total:"<<total;
cout<<endl<<endl<<"Average:"<<total/i;
delete[] p;
return 0;
}
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

support vector regression
support vector regressionsupport vector regression
support vector regression
 
Dma
DmaDma
Dma
 
knn classification
knn classificationknn classification
knn classification
 
Grid search (parameter tuning)
Grid search (parameter tuning)Grid search (parameter tuning)
Grid search (parameter tuning)
 
random forest regression
random forest regressionrandom forest regression
random forest regression
 
K fold
K foldK fold
K fold
 
polynomial linear regression
polynomial linear regressionpolynomial linear regression
polynomial linear regression
 
logistic regression with python and R
logistic regression with python and Rlogistic regression with python and R
logistic regression with python and R
 
multiple linear regression
multiple linear regressionmultiple linear regression
multiple linear regression
 
simple linear regression
simple linear regressionsimple linear regression
simple linear regression
 
SupportVectorRegression
SupportVectorRegressionSupportVectorRegression
SupportVectorRegression
 
Program activation records
Program activation recordsProgram activation records
Program activation records
 
Vector
VectorVector
Vector
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, Hereford
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Stack and Heap
Stack and HeapStack and Heap
Stack and Heap
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
 

Ähnlich wie Memory management

ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptxAshirwad2
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxhelpme43
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2Prerna Sharma
 
Dma
DmaDma
DmaAcad
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTAkhilMishra50
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CPShubham Sinha
 

Ähnlich wie Memory management (20)

Pointers
PointersPointers
Pointers
 
Operators
OperatorsOperators
Operators
 
ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptx
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
PPT DMA.pptx
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer
PointerPointer
Pointer
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptx
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
06 linked list
06 linked list06 linked list
06 linked list
 
Dma
DmaDma
Dma
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 

Mehr von sana younas

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective peoplesana younas
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphssana younas
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
circular linklist
circular linklistcircular linklist
circular linklistsana younas
 
Enterpise system
Enterpise systemEnterpise system
Enterpise systemsana younas
 
Database administration
Database administrationDatabase administration
Database administrationsana younas
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gatesana younas
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingsana younas
 

Mehr von sana younas (16)

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective people
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphs
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Binary search
Binary searchBinary search
Binary search
 
circular linklist
circular linklistcircular linklist
circular linklist
 
Link list 2
Link list 2Link list 2
Link list 2
 
Link list 1
Link list 1Link list 1
Link list 1
 
Heapsort 1
Heapsort 1Heapsort 1
Heapsort 1
 
Arrays
ArraysArrays
Arrays
 
Enterpise system
Enterpise systemEnterpise system
Enterpise system
 
Database administration
Database administrationDatabase administration
Database administration
 
Encoders
EncodersEncoders
Encoders
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gate
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Parallel adders
Parallel addersParallel adders
Parallel adders
 

Kürzlich hochgeladen

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 

Kürzlich hochgeladen (20)

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 

Memory management

  • 2. MEMORY MANAGEMENT  Dynamic and static memory allocation  New operator  Delete operator Presented by:
  • 3.  Memory management is the process of controlling and coordinating, computer memory assigning portions called blocks to various running programs to optimize overall system performance.  Memory management resides in hardware, in the OS (operating system), and in programs and applications. MEMORY MANAGEMENT
  • 4.  Memory is allocated for the declared variable by the compiler. The memory is allocated during compile time. Since most of the declared variables have static memory, this kind of assigning the address of a variable to a pointer is known as static memory allocation. The lifetime of a variable in static memory is the lifetime of the program. STATIC MEMORY ALLOCATION
  • 5.  Allocation of memory at the time of execution (run time) is known as dynamic memory allocation. Dynamic allocation is achieved using certain functions like malloc(), calloc(), realloc(), free in C and operators like "new", "delete" in C++. Dynamic allocation of memory space is done by using these functions when value is returned by functions and assigned to pointer variables. DYNAMIC MEMORY ALLOCATION
  • 6.  The new operator denotes a request for memory allocation at run time. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.  Syntax to use new operator: pointer-variable = new data-type; int *p; p = new int; NEW OPERATOR
  • 7.  Initialize memory:We can initialize the memory using new operator. pointer-variable = new data-type(value); Example: int *p = new int(25); float *p = new float(75.25); Char *p=new char(‘A’); NEW OPERATOR (CONTINUE)
  • 8.  Combine declaration of pointer and their assignment int *p = new int; Example: int *p = new int; float *p = new float; NEW OPERATOR (CONTINUE)
  • 9.  Allocate block of memory: new operator is also used to allocate a block(an array) of memory of type data- type. pointer-variable = new data-type[size]; int *p = new int[10];  Dynamically allocates memory for 10 integers continuously of type int and returns pointer to the first element of the sequence, which is assigned to p(a pointer). p[0] refers to first element,p[1] refers to second element and so on. NEW OPERATOR (CONTINUE)
  • 10.  When memory allocated by new operator is no longer required, it is freed using operator delete. Syntax: delete pointer-variable; Here, pointer-variable is the pointer that points to the data object created by new. Examples: delete p; delete q; DELETE OPERATOR
  • 11.  To free the dynamically allocated array pointed by pointer-variable, use following form of delete: delete[] pointer-variable; Example: delete[] p; The brackets [] indicates the array has been deleted. If you need to delete a single object then, you don't need to use brackets. DELETE OPERATOR (CONTINUE)
  • 12. #include <iostream> using namespace std; class Box { public: Box() { cout <<“box"<<endl; } }; int main( ) { Box* myBoxArray = new Box[4]; // Box memory allocation delete[] myBoxArray; // Box memory is released or delete return 0; } OUTPUT: box box box box
  • 13. #include <iostream> using namespace std; int main () { int i,n,total=0; int * p; cout << "How many subjects would you like to type? "; cin >> i; p= new int[i]; for (n=0; n<i; n++) { cout <<endl<<"Enter marks in subject "<<1+n<<":"; cin >>*(p+n); or cin>>p[i]; } for (n=0; n<i; n++){ total=total+*(p+n); or total=total+p[i]; } cout<<endl<<"Total:"<<total; cout<<endl<<endl<<"Average:"<<total/i; delete[] p; return 0; }