2
Most read
6
Most read
9
Most read
Dynamic Memory Allocation
Introduction
calloc()
malloc()
realloc()
free()
Introduction
• The process of allocating and freeing memory at run
time (or execution time) is called dynamic memory
allocation.
• Problem with Arrays:
– C requires the no. of elements in an array to be specified
at compile time.
– It is difficult to know the exact size of the array in advance
(prior to execution).
– If the size needed at run time is small than the specified
size, then we have wastage of memory space.
– If the size needed at run time is greater than the specified
size, we will have shortage of memory space.
– Solution: DMA
• In C we have the following four functions for
memory management process:
– malloc()
– calloc()
– realloc()
– free()
• These are defined in header file stdlib.h
Memory Allocation Process
4
malloc()
• It allocates requested size of bytes and returns a
pointer to the first byte of the allocated space.
• Syntax:
p=(data_type *)malloc(size_of_block);
• Here, p is a pointer of type data_type. The
malloc() returns a pointer to the first byte to an
area of memory with size size_of_block.
• E.g.
x=(int*)malloc(100*sizeof(int));
x=(int *)malloc(200);
calloc()
• Unlike malloc(), calloc() accepts two arguments:
no_of_blocks and size_of_each_block so that
calloc() can allocate memory for an array of
elements.
• All bytes allocated using calloc() are initialized to
zero and a pointer to the first byte is returned.
• Syntax:
p=(data_type *)calloc(no_of_blocks, size_of_each_block);
• E.g. x=(int *)calloc(5, 100*sizeof(int));
realloc()
• It is used to modify the size of the previously allocated
space.
• When the size of previously allocated memory space is
not enough or when the size of previously allocated
memory space is larger than needed, we need to use
the realloc() function.
• Syntax: If original allocation has been done as:
p=(data_type *)malloc(size);
then reallocation is done as:
p=(data_type *)realloc(p, new_size);
free()
• It frees previously allocated memory space by
malloc(), calloc(), or realloc() function.
• Syntax: free(pointer_name);
Classwork
• Write a C program to find sum of n elements
entered by user. To perform this program,
allocate memory dynamically using malloc()
function.
• Write a C program to find sum of n elements
entered by user. To perform this program,
allocate memory dynamically using calloc()
function
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)

More Related Content

PPTX
Dynamic memory allocation
PPTX
Dynamic memory allocation in c
PPT
Memory allocation in c
PPTX
Dynamic memory allocation
PPT
Dynamic Memory Allocation
PPTX
Arrays in c
PPTX
Dynamic memory Allocation in c language
PPTX
Dynamic Memory allocation
Dynamic memory allocation
Dynamic memory allocation in c
Memory allocation in c
Dynamic memory allocation
Dynamic Memory Allocation
Arrays in c
Dynamic memory Allocation in c language
Dynamic Memory allocation

What's hot (20)

PPTX
16 dynamic-memory-allocation
PPTX
Garbage collection
PPTX
Memory organization in computer architecture
PPTX
C dynamic ppt
PPT
Array in c
PPT
9. Input Output in java
PPT
Pointers C programming
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Strings in Java
PPTX
Programming in c Arrays
PPT
Java Streams
PPTX
PPT
Input and output in C++
PPTX
File in C language
PPTX
Pointer in C++
PPT
Multidimensional array in C
PPTX
Dynamic Memory Allocation in C
PPTX
Function overloading and overriding
PPTX
Array Of Pointers
PPTX
File Handling Python
16 dynamic-memory-allocation
Garbage collection
Memory organization in computer architecture
C dynamic ppt
Array in c
9. Input Output in java
Pointers C programming
Array Introduction One-dimensional array Multidimensional array
Strings in Java
Programming in c Arrays
Java Streams
Input and output in C++
File in C language
Pointer in C++
Multidimensional array in C
Dynamic Memory Allocation in C
Function overloading and overriding
Array Of Pointers
File Handling Python
Ad

Viewers also liked (19)

PPTX
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
PPTX
Dynamic memory allocation in c++
PDF
I/O in java Part 1
PPT
Dynamic memory allocation
PPTX
Java Applets
PPT
User defined functions in C programmig
PPT
File handling in c
PPTX
Network Layer
PPTX
Central processing unit
PPTX
Strings in C
PPT
Chapter 8 - Main Memory
PPS
PPTX
Part I:Introduction to assembly language
PPSX
C Programming : Arrays
PPTX
Array in C
PPT
Os Swapping, Paging, Segmentation and Virtual Memory
PPTX
Array in c language
PDF
Memory management
PPT
Assembly Language Programming Of 8085
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation in c++
I/O in java Part 1
Dynamic memory allocation
Java Applets
User defined functions in C programmig
File handling in c
Network Layer
Central processing unit
Strings in C
Chapter 8 - Main Memory
Part I:Introduction to assembly language
C Programming : Arrays
Array in C
Os Swapping, Paging, Segmentation and Virtual Memory
Array in c language
Memory management
Assembly Language Programming Of 8085
Ad

Similar to Dynamic Memory Allocation(DMA) (20)

PPTX
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
dynamic_v1-3.pptx
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PDF
Dynamic memory allocation
DOCX
Dma
PPTX
Dynamic Memory Allocation in C programming
PPSX
4 dynamic memory allocation
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PDF
Data Structure - Dynamic Memory Allocation
PPTX
Dynamic memory allocation
PPTX
final GROUP 4.pptx
PPT
Dynamic memory allocation
DOCX
DS UNIT3_LINKED LISTS.docx
PPTX
Dynamic memory allocation
PPTX
dynamicmemoryallocation.pptx
PPTX
Memory allocation in c
PPTX
Dynamic memory allocation
CLanguage_ClassPPT_3110003_unit 9Material.ppt
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
dynamic_v1-3.pptx
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
Dynamic memory allocation
Dma
Dynamic Memory Allocation in C programming
4 dynamic memory allocation
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Data Structure - Dynamic Memory Allocation
Dynamic memory allocation
final GROUP 4.pptx
Dynamic memory allocation
DS UNIT3_LINKED LISTS.docx
Dynamic memory allocation
dynamicmemoryallocation.pptx
Memory allocation in c
Dynamic memory allocation

More from Kamal Acharya (20)

PPTX
Programming the basic computer
PPTX
Computer Arithmetic
PPTX
Introduction to Computer Security
PPTX
Session and Cookies
PPTX
Functions in php
PPTX
Web forms in php
PPTX
Making decision and repeating in PHP
PPTX
Working with arrays in php
PPTX
Text and Numbers (Data Types)in PHP
PPTX
Introduction to PHP
PPTX
Capacity Planning of Data Warehousing
PPTX
Data Warehousing
PPTX
Search Engines
PPTX
Web Mining
PPTX
Information Privacy and Data Mining
PPTX
Cluster Analysis
PPTX
Association Analysis in Data Mining
PPTX
Classification techniques in data mining
PPTX
Data Preprocessing
PPTX
Introduction to Data Mining and Data Warehousing
Programming the basic computer
Computer Arithmetic
Introduction to Computer Security
Session and Cookies
Functions in php
Web forms in php
Making decision and repeating in PHP
Working with arrays in php
Text and Numbers (Data Types)in PHP
Introduction to PHP
Capacity Planning of Data Warehousing
Data Warehousing
Search Engines
Web Mining
Information Privacy and Data Mining
Cluster Analysis
Association Analysis in Data Mining
Classification techniques in data mining
Data Preprocessing
Introduction to Data Mining and Data Warehousing

Recently uploaded (20)

PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
advance database management system book.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Trump Administration's workforce development strategy
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
HVAC Specification 2024 according to central public works department
PDF
International_Financial_Reporting_Standa.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
20th Century Theater, Methods, History.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
History, Philosophy and sociology of education (1).pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
advance database management system book.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Trump Administration's workforce development strategy
TNA_Presentation-1-Final(SAVE)) (1).pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Unit 4 Computer Architecture Multicore Processor.pptx
What if we spent less time fighting change, and more time building what’s rig...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Virtual and Augmented Reality in Current Scenario
HVAC Specification 2024 according to central public works department
International_Financial_Reporting_Standa.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
20th Century Theater, Methods, History.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
History, Philosophy and sociology of education (1).pptx

Dynamic Memory Allocation(DMA)

  • 2. Introduction • The process of allocating and freeing memory at run time (or execution time) is called dynamic memory allocation. • Problem with Arrays: – C requires the no. of elements in an array to be specified at compile time. – It is difficult to know the exact size of the array in advance (prior to execution). – If the size needed at run time is small than the specified size, then we have wastage of memory space. – If the size needed at run time is greater than the specified size, we will have shortage of memory space. – Solution: DMA
  • 3. • In C we have the following four functions for memory management process: – malloc() – calloc() – realloc() – free() • These are defined in header file stdlib.h
  • 5. malloc() • It allocates requested size of bytes and returns a pointer to the first byte of the allocated space. • Syntax: p=(data_type *)malloc(size_of_block); • Here, p is a pointer of type data_type. The malloc() returns a pointer to the first byte to an area of memory with size size_of_block. • E.g. x=(int*)malloc(100*sizeof(int)); x=(int *)malloc(200);
  • 6. calloc() • Unlike malloc(), calloc() accepts two arguments: no_of_blocks and size_of_each_block so that calloc() can allocate memory for an array of elements. • All bytes allocated using calloc() are initialized to zero and a pointer to the first byte is returned. • Syntax: p=(data_type *)calloc(no_of_blocks, size_of_each_block); • E.g. x=(int *)calloc(5, 100*sizeof(int));
  • 7. realloc() • It is used to modify the size of the previously allocated space. • When the size of previously allocated memory space is not enough or when the size of previously allocated memory space is larger than needed, we need to use the realloc() function. • Syntax: If original allocation has been done as: p=(data_type *)malloc(size); then reallocation is done as: p=(data_type *)realloc(p, new_size);
  • 8. free() • It frees previously allocated memory space by malloc(), calloc(), or realloc() function. • Syntax: free(pointer_name);
  • 9. Classwork • Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc() function. • Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc() function