SlideShare a Scribd company logo
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

More Related Content

What's hot

support vector regression
support vector regressionsupport vector regression
support vector regression
Akhilesh Joshi
 
Dma
DmaDma
knn classification
knn classificationknn classification
knn classification
Akhilesh Joshi
 
Grid search (parameter tuning)
Grid search (parameter tuning)Grid search (parameter tuning)
Grid search (parameter tuning)
Akhilesh Joshi
 
random forest regression
random forest regressionrandom forest regression
random forest regression
Akhilesh Joshi
 
K fold
K foldK fold
polynomial linear regression
polynomial linear regressionpolynomial linear regression
polynomial linear regression
Akhilesh Joshi
 
logistic regression with python and R
logistic regression with python and Rlogistic regression with python and R
logistic regression with python and R
Akhilesh Joshi
 
multiple linear regression
multiple linear regressionmultiple linear regression
multiple linear regression
Akhilesh Joshi
 
simple linear regression
simple linear regressionsimple linear regression
simple linear regression
Akhilesh Joshi
 
SupportVectorRegression
SupportVectorRegressionSupportVectorRegression
SupportVectorRegressionDaniel K
 
Program activation records
Program activation recordsProgram activation records
Program activation records
Nitin Reddy Katkam
 
Vector
VectorVector
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 structuresJohn(Qiang) Zhang
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, Hereford
Kit Eason
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
Joyjit Choudhury
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
CGC Technical campus,Mohali
 
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
Simone Di Maulo
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06Niit Care
 

What's hot (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
 

Similar to Memory management

Pointers
PointersPointers
ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptx
Ashirwad2
 
8 Pointers
8 Pointers8 Pointers
PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Pointer
PointerPointer
Pointer
manish840
 
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
helpme43
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
06 linked list
06 linked list06 linked list
06 linked list
Rajan Gautam
 
Dma
DmaDma
Dma
Acad
 
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
Selvaraj Seerangan
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
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
AkhilMishra50
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
ShriKant Vashishtha
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
Sabaunnisa3
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
Shubham Sinha
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
Prof .Pragati Khade
 

Similar to 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
 

More from sana younas

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

More from 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
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.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; }