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

What's hot

Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Burhanuddin Kapadia
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Prabhu Govind
 
Stack & heap
Stack & heap Stack & heap
Stack & heap
Shajahan T S Shah
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
vaani pathak
 
Stack and heap
Stack and heapStack and heap
Stack and heap allocation
Stack and heap allocationStack and heap allocation
Stack and heap allocation
ankitbhasin23
 
Memory allocation (4)
Memory allocation (4)Memory allocation (4)
Memory allocation (4)
rockymani
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Memory allocation
Memory allocationMemory allocation
Memory allocation
sanya6900
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Naveen Gupta
 
memory
memorymemory
memory
teach4uin
 
06 linked list
06 linked list06 linked list
06 linked list
Rajan Gautam
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
Ganesh Samarthyam
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data Frames
Work-Bench
 
I Don't Want to Be a Dummy! Encoding Predictors for Trees
I Don't Want to Be a Dummy! Encoding Predictors for TreesI Don't Want to Be a Dummy! Encoding Predictors for Trees
I Don't Want to Be a Dummy! Encoding Predictors for Trees
Work-Bench
 
Heap Management
Heap ManagementHeap Management
Heap Management
Jenny Galino
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
Mandeep Singh
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 

What's hot (20)

Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Stack & heap
Stack & heap Stack & heap
Stack & heap
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Stack and heap allocation
Stack and heap allocationStack and heap allocation
Stack and heap allocation
 
Memory allocation (4)
Memory allocation (4)Memory allocation (4)
Memory allocation (4)
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
memory
memorymemory
memory
 
06 linked list
06 linked list06 linked list
06 linked list
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data Frames
 
I Don't Want to Be a Dummy! Encoding Predictors for Trees
I Don't Want to Be a Dummy! Encoding Predictors for TreesI Don't Want to Be a Dummy! Encoding Predictors for Trees
I Don't Want to Be a Dummy! Encoding Predictors for Trees
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 

Similar to Dynamic memory allocation

Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
Simplilearn
 
Dma
DmaDma
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
Grishma Rajput
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
babuk110
 
(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
Nico Ludwig
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
Shubham Sinha
 
Virtual Method Table and accident prevention
Virtual Method Table and accident preventionVirtual Method Table and accident prevention
Virtual Method Table and accident prevention
Andrey Karpov
 
C interview questions
C interview questionsC interview questions
C interview questions
Soba Arjun
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
Hatem Abd El-Salam
 
dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
ngonidzashemutsipa
 
Dma
DmaDma
Dma
Acad
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptx
helpme43
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlowIntroducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
Etsuji Nakai
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
AkhilMishra50
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
Niharika606186
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
VcTrn1
 
Oracle Database InMemory
Oracle Database InMemoryOracle Database InMemory
Oracle Database InMemory
Jorge Barba
 

Similar to Dynamic memory allocation (20)

Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
 
Dma
DmaDma
Dma
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - 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 in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
 
Virtual Method Table and accident prevention
Virtual Method Table and accident preventionVirtual Method Table and accident prevention
Virtual Method Table and accident prevention
 
C interview questions
C interview questionsC interview questions
C interview questions
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
 
dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
Dma
DmaDma
Dma
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptxPF UE LEC 7 Pointers programming fundamentals (2).pptx
PF UE LEC 7 Pointers programming fundamentals (2).pptx
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlowIntroducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
 
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
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
 
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
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
Oracle Database InMemory
Oracle Database InMemoryOracle Database InMemory
Oracle Database InMemory
 

More from MI RAKIB

Car Detection
Car DetectionCar Detection
Car Detection
MI RAKIB
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
MI RAKIB
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
MI RAKIB
 
Accounting presentation
Accounting presentationAccounting presentation
Accounting presentation
MI RAKIB
 
Infrared detector
Infrared detector Infrared detector
Infrared detector
MI RAKIB
 
Bussiness application design
Bussiness application designBussiness application design
Bussiness application design
MI RAKIB
 
Impacts of rohingya refugee.
Impacts of rohingya refugee.Impacts of rohingya refugee.
Impacts of rohingya refugee.
MI RAKIB
 
Computer Generation
Computer GenerationComputer Generation
Computer Generation
MI RAKIB
 
Aladdin and the Wonderful Lamp
Aladdin and the Wonderful LampAladdin and the Wonderful Lamp
Aladdin and the Wonderful Lamp
MI RAKIB
 

More from MI RAKIB (9)

Car Detection
Car DetectionCar Detection
Car Detection
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
 
Accounting presentation
Accounting presentationAccounting presentation
Accounting presentation
 
Infrared detector
Infrared detector Infrared detector
Infrared detector
 
Bussiness application design
Bussiness application designBussiness application design
Bussiness application design
 
Impacts of rohingya refugee.
Impacts of rohingya refugee.Impacts of rohingya refugee.
Impacts of rohingya refugee.
 
Computer Generation
Computer GenerationComputer Generation
Computer Generation
 
Aladdin and the Wonderful Lamp
Aladdin and the Wonderful LampAladdin and the Wonderful Lamp
Aladdin and the Wonderful Lamp
 

Recently uploaded

A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 

Recently uploaded (20)

A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 

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