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

Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Kamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in cMahesh Tibrewal
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocationGrishma Rajput
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming Kamal Acharya
 
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 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 

What's hot (20)

Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
 
5bit field
5bit field5bit field
5bit field
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
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 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 

Similar to C dynamic ppt

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
 

Similar to C dynamic ppt (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
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
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
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
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
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
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
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

C dynamic ppt

  • 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