SlideShare a Scribd company logo
1 of 16
DYNAMIC MEMORY
ALLOCATION
BY
Mehul Gadhiya
Road Map
• Memory and Allocation
• Memory Allocation
• Why memory Allocation is required?
• Memory Layout
• Stack Memory
• Heap Memory
• Type of Memory Allocation
• Malloc()
• Calloc()
• Memory error
• Memory Leak
• Free()
• Realloc()
• References
Memory and Allocation
Memory: Memory is the processes by which information is encoded, stored and retrieved.
Encoding allow information that is from the outside world to reach our senses in the forms of
chemical and physical stimuli.
Allocation: An alloation is something that you set aside for use .for instance if you want to set
aside a certain amount of hard drive space for an application,you can allocate how much in the
settings
Memory Allocation
• The placement of blocks of information in a memory system is called memory allocation.
• To allocate memory it is necessary to keep information of available memory in the system. If
memory Management system finds sufficient free memory, it allocates only as much memory as
needed ,keeping the rest available to satisfy the future future request .
• If sufficient memory is not available, swapping of blocks is done.
Why memory management is required?
• To provide the memory space to enable several processes to execute concurrently
• To provide a satisfactory level of performance for users
• To protect processes from each other
• To enable sharing of memory space between processes
• To make addressing transparent
Memory Layout
Stack Memory
• Variables created on the stack will go out of scope and automatically deallocate.
• Much faster to allocate in comparison to variables on the heap.
• Stores local data, return addresses, used for parameter passing
• Can have a stack overflow when too much of the stack is used. (recursion, very large
allocations)
• Data created on the stack can be used without pointers.
• You would use the stack if you know exactly how much data you need to allocate before
compile time and it is not too big.
• Usually has a maximum size already determined when your program starts.
Heap Memory
• The heap contains free blocks. New allocations on the heap (by malloc) are satisfied by
creating a suitable block from one of the free blocks.
• The size of the heap is set on application startup, but can grow as space is needed.
• Variables on the heap must be destroyed manually and never fall out of scope. The data is freed
with free
• Slower to allocate in comparison to variables on the stack.
• Used on demand to allocate a block of data for use by the program.
• Can have fragmentation when there are a lot of allocations and deallocations
• You would use the heap if you don’t know exactly how much data you will need at runtime or if
you need to allocate a lot of data.
• Responsible for memory leaks
Types of memory allocation
Static memory allocation:
• In static memory allocation, size of the memory may be required for the calculation that must be
define before loading and executing the program.
Dynamic memory allocation:
• In the Dynamic memory allocation , the memory is allocated to a variable or program at the run
time.
• The only way to access this dynamically allocated memory is through pointer.
• Dynamic memory allocation function:
 Malloc()
 Calloc()
 Free()
 Realloc()
Malloc()
• A block of memory may be allocated using the function malloc. The malloc function reserves a
block of memory of specified size and returns a pointer of type void. This means that we can
assign it to any type of pointer. It takes the following form:
ptr=(cast-type*)malloc(byte-size);
• ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory
with size byte-size.
Example:
x=(int*)malloc(100*sizeof(int));
• On successful execution of this statement a memory equivalent to 100 times the area of int
bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x
of type int.
Calloc()
• Calloc is another memory allocation function that is normally used to request multiple blocks of
storage each of the same size and then sets all bytes to zero. The general form of calloc is:
ptr=(cast-type*) calloc(n,elem-size);
• calloc() allocates contiguous space for n blocks each size of elements size bytes. All bytes are
initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not
enough space a null pointer is returned.
Example:
x=(struct student*)calloc(5,sizeof(struct student));
• Struct student having three member name, age and id number, the calloc allocate the memory to
whole data for 5 such records.
Memory errors
• Using memory that you have not initialized.
• Using memory that you do not own.
• Using more memory than you have allocated.
• Using faulty heap memory management.
• We must be sure that requested memory has been allocated succesfully before using the pointer
x this may be done as follows:
If(x==NULL)
{
printf(“available memory is not sufficient”);
exit(1);
}
Memory leak
• Memory leak occurs when programmers create a memory in heap and forget to delete it.
• Memory leaks are particularly serious issues for programs which definition never terminate.
Example:
/* Function with memory leak */
#include <stdlib.h>
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
return; /* Return without freeing ptr*/
}
Free()
• Compile time storage of a variable is allocated and released by the system in accordance with
its storage class. With the dynamic runtime allocation, it is our responsibility to release the space
when it is not required.
free(ptr);
ptr is a pointer that has been created by using malloc or calloc
• To avoid memory leaks, memory allocated on heap should always be freed when no longer
needed.
/* Function without memory leak */
#include <stdlib.h>;
void f()
{
int ptr = (int ) malloc(sizeof(int));
/* Do some work */
free(ptr);
return;
}
Realloc()
• The memory allocated by using calloc or malloc might be insufficient or excess sometimes in
both the situations we can change the memory size already allocated with the help of the function
realloc. This process is called reallocation of memory. The general statement of reallocation of
memory is :
ptr=realloc(ptr,newsize);
Example:
#include <stdlib.h>;
void f()
{
str = (char *) malloc(15);
str = (char *) realloc(str, 25);
return;
}
Refrences
• Programming in ANSI C [E Balaguruswamy],4edition, ISBN:13 978-0-07-064822-7.
• http://www.geeksforgeeks.org/memory-layout-of-c-program/.
• http://www.tenouk.com/ModuleZ.html

More Related Content

What's hot

MemoryManagementStrategies.ppt
MemoryManagementStrategies.pptMemoryManagementStrategies.ppt
MemoryManagementStrategies.pptPaulRajasingh2
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in cMahesh Tibrewal
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementRahul Jamwal
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocationGrishma Rajput
 
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
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Mangalayatan university
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnTypeMuhammad Hammad Waseem
 
Processor organization &amp; register organization
Processor organization &amp; register organizationProcessor organization &amp; register organization
Processor organization &amp; register organizationGhanshyam Patel
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory managementrprajat007
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSvampugani
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory ManagementAkmal Cikmat
 

What's hot (20)

MemoryManagementStrategies.ppt
MemoryManagementStrategies.pptMemoryManagementStrategies.ppt
MemoryManagementStrategies.ppt
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
loaders and linkers
 loaders and linkers loaders and linkers
loaders and linkers
 
FreeRTOS Course - Queue Management
FreeRTOS Course - Queue ManagementFreeRTOS Course - Queue Management
FreeRTOS Course - Queue Management
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
Processor organization &amp; register organization
Processor organization &amp; register organizationProcessor organization &amp; register organization
Processor organization &amp; register organization
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
Loaders
LoadersLoaders
Loaders
 
Unit 5
Unit 5Unit 5
Unit 5
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Macro Processor
Macro ProcessorMacro Processor
Macro Processor
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory Management
 

Similar to DYNAMIC MEMORY ALLOCATION TECHNIQUES

4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGaurav Mandal
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationUTTAM VERMA
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxNiharika606186
 
Dma
DmaDma
DmaAcad
 
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
 
Heap Memory Management.pptx
Heap Memory Management.pptxHeap Memory Management.pptx
Heap Memory Management.pptxViji B
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
 
GLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingGLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingArun Kumar
 
Computer organization memory hierarchy
Computer organization memory hierarchyComputer organization memory hierarchy
Computer organization memory hierarchyAJAL A J
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVArti Parab Academics
 
Memory organization.pptx
Memory organization.pptxMemory organization.pptx
Memory organization.pptxRamanRay105
 
4 memory management bb
4   memory management bb4   memory management bb
4 memory management bbShahid Riaz
 

Similar to DYNAMIC MEMORY ALLOCATION TECHNIQUES (20)

4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
 
Dma
DmaDma
Dma
 
final GROUP 4.pptx
final GROUP 4.pptxfinal GROUP 4.pptx
final GROUP 4.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
 
Heap Memory Management.pptx
Heap Memory Management.pptxHeap Memory Management.pptx
Heap Memory Management.pptx
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
GLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meetingGLA University is inviting you to a scheduled Zoom meeting
GLA University is inviting you to a scheduled Zoom meeting
 
Computer organization memory hierarchy
Computer organization memory hierarchyComputer organization memory hierarchy
Computer organization memory hierarchy
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
computer-memory
computer-memorycomputer-memory
computer-memory
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Internet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IVInternet of Things, TYBSC IT, Semester 5, Unit IV
Internet of Things, TYBSC IT, Semester 5, Unit IV
 
Memory organization.pptx
Memory organization.pptxMemory organization.pptx
Memory organization.pptx
 
4 memory management bb
4   memory management bb4   memory management bb
4 memory management bb
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

DYNAMIC MEMORY ALLOCATION TECHNIQUES

  • 2. Road Map • Memory and Allocation • Memory Allocation • Why memory Allocation is required? • Memory Layout • Stack Memory • Heap Memory • Type of Memory Allocation • Malloc() • Calloc() • Memory error • Memory Leak • Free() • Realloc() • References
  • 3. Memory and Allocation Memory: Memory is the processes by which information is encoded, stored and retrieved. Encoding allow information that is from the outside world to reach our senses in the forms of chemical and physical stimuli. Allocation: An alloation is something that you set aside for use .for instance if you want to set aside a certain amount of hard drive space for an application,you can allocate how much in the settings
  • 4. Memory Allocation • The placement of blocks of information in a memory system is called memory allocation. • To allocate memory it is necessary to keep information of available memory in the system. If memory Management system finds sufficient free memory, it allocates only as much memory as needed ,keeping the rest available to satisfy the future future request . • If sufficient memory is not available, swapping of blocks is done.
  • 5. Why memory management is required? • To provide the memory space to enable several processes to execute concurrently • To provide a satisfactory level of performance for users • To protect processes from each other • To enable sharing of memory space between processes • To make addressing transparent
  • 7. Stack Memory • Variables created on the stack will go out of scope and automatically deallocate. • Much faster to allocate in comparison to variables on the heap. • Stores local data, return addresses, used for parameter passing • Can have a stack overflow when too much of the stack is used. (recursion, very large allocations) • Data created on the stack can be used without pointers. • You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big. • Usually has a maximum size already determined when your program starts.
  • 8. Heap Memory • The heap contains free blocks. New allocations on the heap (by malloc) are satisfied by creating a suitable block from one of the free blocks. • The size of the heap is set on application startup, but can grow as space is needed. • Variables on the heap must be destroyed manually and never fall out of scope. The data is freed with free • Slower to allocate in comparison to variables on the stack. • Used on demand to allocate a block of data for use by the program. • Can have fragmentation when there are a lot of allocations and deallocations • You would use the heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data. • Responsible for memory leaks
  • 9. Types of memory allocation Static memory allocation: • In static memory allocation, size of the memory may be required for the calculation that must be define before loading and executing the program. Dynamic memory allocation: • In the Dynamic memory allocation , the memory is allocated to a variable or program at the run time. • The only way to access this dynamically allocated memory is through pointer. • Dynamic memory allocation function:  Malloc()  Calloc()  Free()  Realloc()
  • 10. Malloc() • A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form: ptr=(cast-type*)malloc(byte-size); • ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size. Example: x=(int*)malloc(100*sizeof(int)); • On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int.
  • 11. Calloc() • Calloc is another memory allocation function that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero. The general form of calloc is: ptr=(cast-type*) calloc(n,elem-size); • calloc() allocates contiguous space for n blocks each size of elements size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned. Example: x=(struct student*)calloc(5,sizeof(struct student)); • Struct student having three member name, age and id number, the calloc allocate the memory to whole data for 5 such records.
  • 12. Memory errors • Using memory that you have not initialized. • Using memory that you do not own. • Using more memory than you have allocated. • Using faulty heap memory management. • We must be sure that requested memory has been allocated succesfully before using the pointer x this may be done as follows: If(x==NULL) { printf(“available memory is not sufficient”); exit(1); }
  • 13. Memory leak • Memory leak occurs when programmers create a memory in heap and forget to delete it. • Memory leaks are particularly serious issues for programs which definition never terminate. Example: /* Function with memory leak */ #include <stdlib.h> void f() { int *ptr = (int *) malloc(sizeof(int)); /* Do some work */ return; /* Return without freeing ptr*/ }
  • 14. Free() • Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required. free(ptr); ptr is a pointer that has been created by using malloc or calloc • To avoid memory leaks, memory allocated on heap should always be freed when no longer needed. /* Function without memory leak */ #include <stdlib.h>; void f() { int ptr = (int ) malloc(sizeof(int)); /* Do some work */ free(ptr); return; }
  • 15. Realloc() • The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function realloc. This process is called reallocation of memory. The general statement of reallocation of memory is : ptr=realloc(ptr,newsize); Example: #include <stdlib.h>; void f() { str = (char *) malloc(15); str = (char *) realloc(str, 25); return; }
  • 16. Refrences • Programming in ANSI C [E Balaguruswamy],4edition, ISBN:13 978-0-07-064822-7. • http://www.geeksforgeeks.org/memory-layout-of-c-program/. • http://www.tenouk.com/ModuleZ.html