SlideShare a Scribd company logo
01010010
01000001
01001011
01001001
01000010
01010010
01000001
01001011
01001001
01000010
Dynamic Memory Allocation
Dynamic memory allocation refers to performing
manual memory management for dynamic
memory allocation in the C programming language
via a group of functions in the C standard library.
C Dynamic Memory Allocation
01010010 01000001
01001011 01001001
01000010
Why Dynamic Memory Allocation ?
In many situations the
programmer requires
greater flexibility in
managing the lifetime of
allocated memory.
01010010 01000001
01001011 01001001
01000010
As you know, an array is a collection of a fixed number of
values. Once the size of an array is declared, you cannot change
it.
Sometimes the size of the array you declared may be
insufficient. To solve this issue, you can allocate memory
manually during run-time. This is known as dynamic memory
allocation in C programming.
01010010 01000001
01001011 01001001
01000010
Overview of functions
Function Description
malloc() allocates the specified number of bytes
realloc() increase or decrease the size of the specific block of
memory , moving it if necessary
calloc() allocates the number of bytes and initialize them to zero
free() releases the specified block of memory back to system
The C dynamic memory allocation functions are defined in stdlib.h
The name "malloc" stands for memory allocation.
C malloc()
01010010 01000001
01001011 01001001
01000010
Syntax of malloc()
Example
The malloc() function reserves a block of memory of the
specified number of bytes. And, it returns a pointer of void
which can be casted into pointers of any form.
The name "calloc" stands for contiguous allocation.
C calloc()
01010010 01000001
01001011 01001001
01000010
Syntax of calloc()
Example
The malloc() function allocates memory and leaves the memory
uninitialized. Whereas, the calloc() function allocates memory
and initializes all bits to zero.
C free()
01010010 01000001
01001011 01001001
01000010
Syntax of free()
Dynamically allocated memory created with either
calloc() or malloc() dosen’t get freed on their own. You
must explicitly use free() to release the space.
This statement frees the space allocated in the memory
pointed by ptr .
C realoc()
If the dynamically allocated memory is insufficient or
more than required, you can change the size of previously
allocated memory using the realloc() function.
Syntax of realloc()
Here ptr is reallocated with a new size x .
01010010 01000001
01001011 01001001
01000010
Usage examples
Creating an array of ten integers with automatic scope is straightforward in
C:
However, the size of the array is fixed at compile time. If one wishes to
allocate a similar array dynamically, the following code can be used:
01010010 01000001
01001011 01001001
01000010
Because malloc() might not be able to service the request , it might be return
a null pointer and it is good programming practice to check this:
When the program no longer needs the dynamic array, it must eventually call
free to return the memory it occupies to the free store:
With realloc we can resize the amount of memory a pointer points
to. For example, if we have a pointer acting as an array of size n
and we want to change it to an array of size m, we can use realloc.
Note that realloc must be assumed to have changed the base address of
the block . 01010010 01000001
01001011 01001001
01000010
Example 1: malloc()
and free()
Here, we have dynamically allocated
the memory for n number of int .
01010010 01000001
01001011 01001001
01000010
Example 2: calloc()
and free()
01010010 01000001
01001011 01001001
01000010
Example 3: realloc()
When you run the program, the output will be:
01010010 01000001
01001011 01001001
01000010
THANK YOU
01010010 01000001
01001011 01001001
01000010
01010010 01000001
01001011 01001001
01000010

More Related Content

PPTX
Dynamic memory allocation in c language
tanmaymodi4
 
PPTX
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Mangalayatan university
 
PPSX
4 dynamic memory allocation
Frijo Francis
 
PPTX
Memory allocation in c
Muhammed Thanveer M
 
PDF
TLPI - 7 Memory Allocation
Shu-Yu Fu
 
PPTX
16 dynamic-memory-allocation
Rohit Shrivastava
 
PPTX
Dynamic memory Allocation in c language
kiran Patel
 
PPTX
C dynamic ppt
RJ Mehul Gadhiya
 
Dynamic memory allocation in c language
tanmaymodi4
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Mangalayatan university
 
4 dynamic memory allocation
Frijo Francis
 
Memory allocation in c
Muhammed Thanveer M
 
TLPI - 7 Memory Allocation
Shu-Yu Fu
 
16 dynamic-memory-allocation
Rohit Shrivastava
 
Dynamic memory Allocation in c language
kiran Patel
 
C dynamic ppt
RJ Mehul Gadhiya
 

What's hot (20)

PPTX
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPTX
Dynamic memory allocation
Burhanuddin Kapadia
 
PPT
Memory allocation in c
Prabhu Govind
 
PPTX
Stack & heap
Shajahan T S Shah
 
PPT
Dynamic Memory Allocation
vaani pathak
 
PPT
Stack and heap allocation
ankitbhasin23
 
PPT
Memory allocation (4)
rockymani
 
PPTX
Functions with heap and stack
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Memory allocation
sanya6900
 
PPT
Dynamic memory allocation
Naveen Gupta
 
PPT
memory
teach4uin
 
PDF
06 linked list
Rajan Gautam
 
PDF
15 Jo P Mar 08
Ganesh Samarthyam
 
PDF
Broom: Converting Statistical Models to Tidy Data Frames
Work-Bench
 
PDF
I Don't Want to Be a Dummy! Encoding Predictors for Trees
Work-Bench
 
PPTX
Heap Management
Jenny Galino
 
PPT
Standard Template Library (STL) in Object Oriented Programming
Mandeep Singh
 
PPTX
Data structure and algorithm
Trupti Agrawal
 
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
Dynamic memory allocation in c
lavanya marichamy
 
Dynamic memory allocation
Burhanuddin Kapadia
 
Memory allocation in c
Prabhu Govind
 
Stack & heap
Shajahan T S Shah
 
Dynamic Memory Allocation
vaani pathak
 
Stack and heap allocation
ankitbhasin23
 
Memory allocation (4)
rockymani
 
Memory allocation
sanya6900
 
Dynamic memory allocation
Naveen Gupta
 
memory
teach4uin
 
06 linked list
Rajan Gautam
 
15 Jo P Mar 08
Ganesh Samarthyam
 
Broom: Converting Statistical Models to Tidy Data Frames
Work-Bench
 
I Don't Want to Be a Dummy! Encoding Predictors for Trees
Work-Bench
 
Heap Management
Jenny Galino
 
Standard Template Library (STL) in Object Oriented Programming
Mandeep Singh
 
Data structure and algorithm
Trupti Agrawal
 
Ad

Similar to Dynamic memory allocation (20)

PPTX
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
PPTX
Dynamic Memory allocation
Grishma Rajput
 
PPTX
Dynamic Memory Allocation in C programming
Rhishav Poudyal
 
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
PDF
Data Structure - Dynamic Memory Allocation
babuk110
 
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
PPTX
Dynamic memory allocation
Mohammad Usman
 
PPTX
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
PPTX
final GROUP 4.pptx
ngonidzashemutsipa
 
PPTX
Dynamic memeory allocation DMA (dyunamic momory .pptx
KumaranG23
 
PPTX
Dynamic Memory Allocation in c prog.pptx
meghana092
 
PPTX
Dynamic memory allocation
UTTAM VERMA
 
PPTX
Dynamic Memory Allocation In C
Simplilearn
 
DOCX
Dma
Acad
 
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
PPTX
dynamic_v1-3.pptx
ngonidzashemutsipa
 
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
roykaustav382
 
PPTX
dynamicmemoryallocation.pptx
Niharika606186
 
PPTX
Dynamic Memory Allocation in C Programming | IIES Guide
ibrahimseo2025
 
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
Dynamic Memory allocation
Grishma Rajput
 
Dynamic Memory Allocation in C programming
Rhishav Poudyal
 
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
Data Structure - Dynamic Memory Allocation
babuk110
 
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
Dynamic memory allocation
Mohammad Usman
 
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
final GROUP 4.pptx
ngonidzashemutsipa
 
Dynamic memeory allocation DMA (dyunamic momory .pptx
KumaranG23
 
Dynamic Memory Allocation in c prog.pptx
meghana092
 
Dynamic memory allocation
UTTAM VERMA
 
Dynamic Memory Allocation In C
Simplilearn
 
Dma
Acad
 
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
dynamic_v1-3.pptx
ngonidzashemutsipa
 
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
roykaustav382
 
dynamicmemoryallocation.pptx
Niharika606186
 
Dynamic Memory Allocation in C Programming | IIES Guide
ibrahimseo2025
 
Ad

More from MI RAKIB (9)

PPTX
Car Detection
MI RAKIB
 
PPTX
Error detection and correction
MI RAKIB
 
PPTX
Microprocessor
MI RAKIB
 
PPTX
Accounting presentation
MI RAKIB
 
PPTX
Infrared detector
MI RAKIB
 
PPTX
Bussiness application design
MI RAKIB
 
PPTX
Impacts of rohingya refugee.
MI RAKIB
 
PPTX
Computer Generation
MI RAKIB
 
PPTX
Aladdin and the Wonderful Lamp
MI RAKIB
 
Car Detection
MI RAKIB
 
Error detection and correction
MI RAKIB
 
Microprocessor
MI RAKIB
 
Accounting presentation
MI RAKIB
 
Infrared detector
MI RAKIB
 
Bussiness application design
MI RAKIB
 
Impacts of rohingya refugee.
MI RAKIB
 
Computer Generation
MI RAKIB
 
Aladdin and the Wonderful Lamp
MI RAKIB
 

Recently uploaded (20)

PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Zero Carbon Building Performance standard
BassemOsman1
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 

Dynamic memory allocation

  • 2. Dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library. C Dynamic Memory Allocation 01010010 01000001 01001011 01001001 01000010
  • 3. Why Dynamic Memory Allocation ? In many situations the programmer requires greater flexibility in managing the lifetime of allocated memory. 01010010 01000001 01001011 01001001 01000010
  • 4. As you know, an array is a collection of a fixed number of values. Once the size of an array is declared, you cannot change it. Sometimes the size of the array you declared may be insufficient. To solve this issue, you can allocate memory manually during run-time. This is known as dynamic memory allocation in C programming. 01010010 01000001 01001011 01001001 01000010
  • 5. Overview of functions Function Description malloc() allocates the specified number of bytes realloc() increase or decrease the size of the specific block of memory , moving it if necessary calloc() allocates the number of bytes and initialize them to zero free() releases the specified block of memory back to system The C dynamic memory allocation functions are defined in stdlib.h
  • 6. The name "malloc" stands for memory allocation. C malloc() 01010010 01000001 01001011 01001001 01000010 Syntax of malloc() Example The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form.
  • 7. The name "calloc" stands for contiguous allocation. C calloc() 01010010 01000001 01001011 01001001 01000010 Syntax of calloc() Example The malloc() function allocates memory and leaves the memory uninitialized. Whereas, the calloc() function allocates memory and initializes all bits to zero.
  • 8. C free() 01010010 01000001 01001011 01001001 01000010 Syntax of free() Dynamically allocated memory created with either calloc() or malloc() dosen’t get freed on their own. You must explicitly use free() to release the space. This statement frees the space allocated in the memory pointed by ptr .
  • 9. C realoc() If the dynamically allocated memory is insufficient or more than required, you can change the size of previously allocated memory using the realloc() function. Syntax of realloc() Here ptr is reallocated with a new size x . 01010010 01000001 01001011 01001001 01000010
  • 10. Usage examples Creating an array of ten integers with automatic scope is straightforward in C: However, the size of the array is fixed at compile time. If one wishes to allocate a similar array dynamically, the following code can be used: 01010010 01000001 01001011 01001001 01000010
  • 11. Because malloc() might not be able to service the request , it might be return a null pointer and it is good programming practice to check this: When the program no longer needs the dynamic array, it must eventually call free to return the memory it occupies to the free store:
  • 12. With realloc we can resize the amount of memory a pointer points to. For example, if we have a pointer acting as an array of size n and we want to change it to an array of size m, we can use realloc. Note that realloc must be assumed to have changed the base address of the block . 01010010 01000001 01001011 01001001 01000010
  • 13. Example 1: malloc() and free() Here, we have dynamically allocated the memory for n number of int . 01010010 01000001 01001011 01001001 01000010
  • 14. Example 2: calloc() and free() 01010010 01000001 01001011 01001001 01000010
  • 15. Example 3: realloc() When you run the program, the output will be: 01010010 01000001 01001011 01001001 01000010
  • 16. THANK YOU 01010010 01000001 01001011 01001001 01000010 01010010 01000001 01001011 01001001 01000010