SlideShare a Scribd company logo
1 of 9
Dynamic Memory allocation
• Every program needs storage to be allocated to it for
various purposes.
• When you write a program, you specify what storage
it needs by declaring variables and instances of
classes, e.g:
int a,b,c;
float nums[100];
Circle myCircle(2.0,3,3); etc...
• Then when the program executes, it can make use of
this memory
• It is not possible for variables or objects to be added
during the execution of a program
• In order to execute a program, it must be loaded into
RAM (Random Access Memory).
• A certain amount of RAM is allocated as the
permanent storage for your program, and this is where
the global variables are stored.
• A dynamic region of memory called the stack is where
local variables are stored.
• This storage is fixed at compile time.
Remember, global variables hold their value for the
lifetime of the program, while local variables only
hold their value for the lifetime of the function they
are declared in.
Programs and Memory
• However, there are times when it is necessary for a
program to make use of variable amounts of storage.
• For example, you may want to write a program that
reads in a list of numbers (until 999 is entered) and
finds their average.
• You may want to store the numbers in an array, but of
what size?
• Each time the program is run, the user may enter any
number of numbers, maybe 5, 50 or 5000.
• One solution is to declare an array of the maximum
size that it could ever be. E.g int nums[10000].
• However, this is wasteful of memory, as you may very
often use only a fraction of the space allocated.
• The solution is to use dynamic allocation of memory.
• This is the means by which a program can obtain
memory while it is running.
• Powerful support for dynamic memory allocation is
provided in C++, with the operator new.
• Memory allocated by C++'s dynamic allocation
function is obtained from the heap.
• The heap is a region of free memory area that lies
between your program and its permanent storage area
and the stack.
• C++ also provides the operator delete, which releases
the memory once the program doesn't need it
anymore.
The new operator
• The new operator returns a pointer to allocated
memory from the heap.
• It returns a NULL pointer if there is insufficient
memory to fulfill the allocation request.
• The delete operator frees memory previously allocated
using new.
• Example of usage:
int *ptr; //Pointer that can point to an integer
ptr = new int; //Now it points to allocated memory
Example: new
int *ptr; //Pointer that can point to an integer
ptr = new int; //Now it points to allocated memory
if(!ptr) //NULL pointer returned
{
cout << "Allocation errorn";
}
else
{
*p = 100;
cout <<"Memory location: "<< ptr;
cout <<" contains the int value: "<< *ptr <<endl;
delete ptr; //deallocate the memory
}
Example: new with initialisation
int *ptr; //Pointer that can point to an integer
ptr = new int(87);
//Allocate memory and initialize to 87
if(!ptr) //NULL pointer returned
{
cout << "Allocation errorn";
}
else
{
cout <<"Memory location: "<< ptr;
cout <<" contains the int value: "<< *ptr <<endl;
delete ptr; //deallocate the memory
}
Allocating arrays
• You can allocate one-dimensional arrays using new as
follows:
int *ptr;
ptr = new int[10];
• You may then use the pointer as you would a normal
array.
• You may not specify an initialiser when allocating
arrays.
• When an array allocated by using new is released,
delete must be told that it is an array. E.g:
delete [] ptr;
int *ptr;
int inum;
cout<<"How many numbers will you enter?: ";
cin>> inum;
ptr = new int[inum];
if(!ptr)
cout<<" Allocation Error!n";
else
{
for(int i=0;i<inum;i++)
{
cout<< "Enter number: ";
cin>>ptr[i];
}
/*
* .....Do some processing to the array .......
*/
delete [ ] ptr; //Release 10 elements
}
}
Example:
new with arrays.

More Related Content

Similar to GLA University is inviting you to a scheduled Zoom meeting

4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
Dma
DmaDma
DmaAcad
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxNiharika606186
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationUTTAM VERMA
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGaurav Mandal
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxSKUP1
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2Aanand Singh
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 

Similar to GLA University is inviting you to a scheduled Zoom meeting (20)

4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
memory
memorymemory
memory
 
Dma
DmaDma
Dma
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
1. C Basics for Data Structures Bridge Course
1. C Basics for Data Structures   Bridge Course1. C Basics for Data Structures   Bridge Course
1. C Basics for Data Structures Bridge Course
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 

Recently uploaded

CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalSwarnaSLcse
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.benjamincojr
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligencemahaffeycheryld
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationEmaan Sharma
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfKira Dess
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Toolssoginsider
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdfAlexander Litvinenko
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashidFaiyazSheikh
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentationsj9399037128
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxMustafa Ahmed
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualBalamuruganV28
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptjigup7320
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...IJECEIAES
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New HorizonMorshed Ahmed Rahath
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...Amil baba
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfssuser5c9d4b1
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksIJECEIAES
 

Recently uploaded (20)

CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 

GLA University is inviting you to a scheduled Zoom meeting

  • 1. Dynamic Memory allocation • Every program needs storage to be allocated to it for various purposes. • When you write a program, you specify what storage it needs by declaring variables and instances of classes, e.g: int a,b,c; float nums[100]; Circle myCircle(2.0,3,3); etc... • Then when the program executes, it can make use of this memory • It is not possible for variables or objects to be added during the execution of a program
  • 2. • In order to execute a program, it must be loaded into RAM (Random Access Memory). • A certain amount of RAM is allocated as the permanent storage for your program, and this is where the global variables are stored. • A dynamic region of memory called the stack is where local variables are stored. • This storage is fixed at compile time. Remember, global variables hold their value for the lifetime of the program, while local variables only hold their value for the lifetime of the function they are declared in. Programs and Memory
  • 3. • However, there are times when it is necessary for a program to make use of variable amounts of storage. • For example, you may want to write a program that reads in a list of numbers (until 999 is entered) and finds their average. • You may want to store the numbers in an array, but of what size? • Each time the program is run, the user may enter any number of numbers, maybe 5, 50 or 5000. • One solution is to declare an array of the maximum size that it could ever be. E.g int nums[10000]. • However, this is wasteful of memory, as you may very often use only a fraction of the space allocated.
  • 4. • The solution is to use dynamic allocation of memory. • This is the means by which a program can obtain memory while it is running. • Powerful support for dynamic memory allocation is provided in C++, with the operator new. • Memory allocated by C++'s dynamic allocation function is obtained from the heap. • The heap is a region of free memory area that lies between your program and its permanent storage area and the stack. • C++ also provides the operator delete, which releases the memory once the program doesn't need it anymore.
  • 5. The new operator • The new operator returns a pointer to allocated memory from the heap. • It returns a NULL pointer if there is insufficient memory to fulfill the allocation request. • The delete operator frees memory previously allocated using new. • Example of usage: int *ptr; //Pointer that can point to an integer ptr = new int; //Now it points to allocated memory
  • 6. Example: new int *ptr; //Pointer that can point to an integer ptr = new int; //Now it points to allocated memory if(!ptr) //NULL pointer returned { cout << "Allocation errorn"; } else { *p = 100; cout <<"Memory location: "<< ptr; cout <<" contains the int value: "<< *ptr <<endl; delete ptr; //deallocate the memory }
  • 7. Example: new with initialisation int *ptr; //Pointer that can point to an integer ptr = new int(87); //Allocate memory and initialize to 87 if(!ptr) //NULL pointer returned { cout << "Allocation errorn"; } else { cout <<"Memory location: "<< ptr; cout <<" contains the int value: "<< *ptr <<endl; delete ptr; //deallocate the memory }
  • 8. Allocating arrays • You can allocate one-dimensional arrays using new as follows: int *ptr; ptr = new int[10]; • You may then use the pointer as you would a normal array. • You may not specify an initialiser when allocating arrays. • When an array allocated by using new is released, delete must be told that it is an array. E.g: delete [] ptr;
  • 9. int *ptr; int inum; cout<<"How many numbers will you enter?: "; cin>> inum; ptr = new int[inum]; if(!ptr) cout<<" Allocation Error!n"; else { for(int i=0;i<inum;i++) { cout<< "Enter number: "; cin>>ptr[i]; } /* * .....Do some processing to the array ....... */ delete [ ] ptr; //Release 10 elements } } Example: new with arrays.