SlideShare a Scribd company logo
1 of 9
Dynamic Memory Allocation
Active Learning Assessment
Semester:2
Computer Programming Utilization(2110003)
Made By:- Burhanuddin Kapadia Submitted to:- Prof. Parin Patel
Enrolment:- 170120119026
Branch:- Mechanical Department
Contents
๏‚ด Introduction
๏‚ด Memory Allocation process
๏‚ด Malloc
๏‚ด Calloc
๏‚ด Difference between malloc & calloc
๏‚ด References
Introduction
๏‚ด C language requires numbers of elements in an array to be specified at compile time.
๏‚ด Many languages permit a programmer to specify an arrays size at run time.
๏‚ด Such language have the ability to calculate and assign, during execution, the memory
space required by the variables in a program.
๏‚ด The process of allocating memory at run time is known as Dynamic Memory Allocation.
๏‚ด C program have four routine library for memory allocation and freeing space.
i. Malloc
ii. Calloc
iii. Free
iv. realloc
๏‚ด Their performance varies in both execution time and required memory.
Memory Allocation
Local Variables
Free Memory
C program instructions
Global Variables
Stack
Heap
Permanent Storage Area
Malloc
๏‚ด A block of memory is allocated by malloc
๏‚ด Allocates single block of requested memory.
๏‚ด It doesn't initialize memory at execution time, so it has
garbage value initially.
๏‚ด Allocates specified number of bytes
๏‚ด Returns NULL if memory is not sufficient.
๏‚ด Syntax of malloc():
๐’—๐’๐’Š๐’… โˆ— ๐’Ž๐’‚๐’๐’๐’๐’„ ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’ ;
๏‚ด Can be written as:-
๐’‘๐’•๐’“ = ๐’„๐’‚๐’”๐’• โˆ’ ๐’•๐’š๐’‘๐’† โˆ— ๐’Ž๐’‚๐’๐’๐’๐’„ ๐’ƒ๐’š๐’•๐’† โˆ’ ๐’”๐’Š๐’›๐’† ;
Calloc
๏‚ด Calloc is normally used for requesting memory space at run
time.
๏‚ด It stores data types such as arrays and structures.
๏‚ด Allocates multiple block of requested memory.
๏‚ด Calloc allocates memory to hold 30 records.
๏‚ด Allocates specified numbers of bytes.
๏‚ด Initially initialize all bytes to zero.
๏‚ด syntax of calloc():
๐’—๐’๐’Š๐’… โˆ— ๐’„๐’‚๐’๐’๐’๐’„ ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’, ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’”๐’Š๐’›๐’† ;
๏‚ด Can be written as:
๐’‘๐’•๐’“ = ๐’„๐’‚๐’”๐’• โˆ’ ๐’•๐’š๐’‘๐’† โˆ— ๐’„๐’‚๐’๐’๐’๐’„ ๐’, ๐’†๐’๐’†๐’Ž โˆ’ ๐’”๐’Š๐’›๐’† ;
Malloc() Calloc()
๏‚ด The name malloc stands
for memory allocation.
๏‚ด Takes only one argument
๏‚ด Amount of memory is in
bytes
๏‚ด Does not initialize the
memory allocated
๏‚ด The name calloc stands
for contiguous allocation
๏‚ด Takes two argument
๏‚ด Amount of memory of single
variable in numbers of
variable
๏‚ด Guarantees that all the bytes
of allocated memory block
have been initialized to 0
Difference between malloc & calloc
Programs for malloc & calloc
/* Allocate memory for an int. */
int *ptr = (int*) malloc(sizeof
(int));
if (ptr == NULL)
{
printf("Could not allocate
memoryn");
exit(-1);
}
else
printf("Memory allocated
successfully.n");
/* Allocating memory for an array
of 10 elements of type int. */
int *ptr = (int*) calloc(10 ,sizeof (int));
if (ptr == NULL)
{
printf("Could not allocate
memoryn");
exit(-1);
}
else
printf("Memory allocated
successfully.n");
Reference
๏‚ด https://www.javatpoint.com/cpp-tutorial
๏‚ด https://www.studytonight.com/c/dynamic-memory-
allocation-in-c.php
๏‚ด https://www.javatpoint.com/dynamic-memory-allocation-in-c
๏‚ด https://www.geeksforgeeks.org/calloc-versus-malloc/
๏‚ด https://www.programiz.com/c-programming/c-dynamic-
memory-allocation

More Related Content

What's hot

Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
ย 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionKamal Acharya
ย 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
ย 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentationfika sweety
ย 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
ย 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arraysjanani thirupathi
ย 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
ย 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in cMahesh Tibrewal
ย 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
ย 
07 java variables
07   java variables07   java variables
07 java variablesZeeshan-Shaikh
ย 
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 in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languageTanmay Modi
ย 
String in c
String in cString in c
String in cSuneel Dogra
ย 
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
ย 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
ย 
Array in c++
Array in c++Array in c++
Array in c++Mahesha Mano
ย 

What's hot (20)

Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
ย 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
ย 
Function overloading
Function overloadingFunction overloading
Function overloading
ย 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
ย 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
ย 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
ย 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
ย 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
ย 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
ย 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
ย 
07 java variables
07   java variables07   java variables
07 java variables
ย 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
ย 
Functions in c
Functions in cFunctions in c
Functions in c
ย 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
ย 
String in c
String in cString in c
String 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...
ย 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
ย 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
ย 
Array in c++
Array in c++Array in c++
Array in c++
ย 
Function C programming
Function C programmingFunction C programming
Function C programming
ย 

Similar to Dynamic memory allocation

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 allocationUTTAM VERMA
ย 
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
ย 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationShu-Yu Fu
ย 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6Hatem Abd El-Salam
ย 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In CSimplilearn
ย 
Memory manament in C
Memory manament in CMemory manament in C
Memory manament in CVu Dang Ngoc
ย 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxNiharika606186
ย 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
ย 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGaurav Mandal
ย 
Dma
DmaDma
DmaAcad
ย 
Memory management CP
Memory management  CPMemory management  CP
Memory management CPShubham Sinha
ย 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
ย 
C interview questions
C interview questionsC interview questions
C interview questionsSoba Arjun
ย 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversRajKumar Rampelli
ย 

Similar to Dynamic memory allocation (20)

Dma
DmaDma
Dma
ย 
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
ย 
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
ย 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
ย 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
ย 
final GROUP 4.pptx
final GROUP 4.pptxfinal GROUP 4.pptx
final GROUP 4.pptx
ย 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
ย 
Memory manament in C
Memory manament in CMemory manament in C
Memory manament in C
ย 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
ย 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
ย 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
ย 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
ย 
Stack & heap
Stack & heap Stack & heap
Stack & heap
ย 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
ย 
Dma
DmaDma
Dma
ย 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
ย 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
ย 
C interview questions
C interview questionsC interview questions
C interview questions
ย 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device Drivers
ย 

More from Burhanuddin Kapadia

Factors affecting acoustics of buildings and their remedies.
Factors affecting acoustics of buildings and their remedies.Factors affecting acoustics of buildings and their remedies.
Factors affecting acoustics of buildings and their remedies.Burhanuddin Kapadia
ย 
Definition and process of communication
Definition and process of communicationDefinition and process of communication
Definition and process of communicationBurhanuddin Kapadia
ย 
Embarking on the journey of contribution
Embarking on the journey of contributionEmbarking on the journey of contribution
Embarking on the journey of contributionBurhanuddin Kapadia
ย 
Principles and Building Bye Laws
Principles and Building Bye LawsPrinciples and Building Bye Laws
Principles and Building Bye LawsBurhanuddin Kapadia
ย 
Double integral over rectangles, Fubinis theorem, properties of double integral
Double integral over rectangles, Fubinis theorem, properties of double integralDouble integral over rectangles, Fubinis theorem, properties of double integral
Double integral over rectangles, Fubinis theorem, properties of double integralBurhanuddin Kapadia
ย 

More from Burhanuddin Kapadia (9)

Gram-Schmidt process
Gram-Schmidt processGram-Schmidt process
Gram-Schmidt process
ย 
Factors affecting acoustics of buildings and their remedies.
Factors affecting acoustics of buildings and their remedies.Factors affecting acoustics of buildings and their remedies.
Factors affecting acoustics of buildings and their remedies.
ย 
Ilumination schemes
Ilumination schemes Ilumination schemes
Ilumination schemes
ย 
Definition and process of communication
Definition and process of communicationDefinition and process of communication
Definition and process of communication
ย 
Embarking on the journey of contribution
Embarking on the journey of contributionEmbarking on the journey of contribution
Embarking on the journey of contribution
ย 
Principles and Building Bye Laws
Principles and Building Bye LawsPrinciples and Building Bye Laws
Principles and Building Bye Laws
ย 
Air compressor
Air compressorAir compressor
Air compressor
ย 
Double integral over rectangles, Fubinis theorem, properties of double integral
Double integral over rectangles, Fubinis theorem, properties of double integralDouble integral over rectangles, Fubinis theorem, properties of double integral
Double integral over rectangles, Fubinis theorem, properties of double integral
ย 
Electronic Pills
Electronic PillsElectronic Pills
Electronic Pills
ย 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
ย 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
ย 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
ย 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
ย 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
ย 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
ย 
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,Virag Sontakke
ย 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
ย 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
ย 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
ย 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
ย 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
ย 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
ย 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...Dr. Mazin Mohamed alkathiri
ย 
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
ย 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
ย 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
ย 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
ย 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
ย 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
ย 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ย 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
ย 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
ย 
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,
เคญเคพเคฐเคค-เคฐเฅ‹เคฎ เคตเฅเคฏเคพเคชเคพเคฐ.pptx, Indo-Roman Trade,
ย 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
ย 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
ย 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
ย 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
ย 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
ย 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
ย 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
ย 
Model Call Girl in Tilak Nagar Delhi reach out to us at ๐Ÿ”9953056974๐Ÿ”
Model Call Girl in Tilak Nagar Delhi reach out to us at ๐Ÿ”9953056974๐Ÿ”Model Call Girl in Tilak Nagar Delhi reach out to us at ๐Ÿ”9953056974๐Ÿ”
Model Call Girl in Tilak Nagar Delhi reach out to us at ๐Ÿ”9953056974๐Ÿ”
ย 
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
ย 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
ย 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
ย 

Dynamic memory allocation

  • 1. Dynamic Memory Allocation Active Learning Assessment Semester:2 Computer Programming Utilization(2110003) Made By:- Burhanuddin Kapadia Submitted to:- Prof. Parin Patel Enrolment:- 170120119026 Branch:- Mechanical Department
  • 2. Contents ๏‚ด Introduction ๏‚ด Memory Allocation process ๏‚ด Malloc ๏‚ด Calloc ๏‚ด Difference between malloc & calloc ๏‚ด References
  • 3. Introduction ๏‚ด C language requires numbers of elements in an array to be specified at compile time. ๏‚ด Many languages permit a programmer to specify an arrays size at run time. ๏‚ด Such language have the ability to calculate and assign, during execution, the memory space required by the variables in a program. ๏‚ด The process of allocating memory at run time is known as Dynamic Memory Allocation. ๏‚ด C program have four routine library for memory allocation and freeing space. i. Malloc ii. Calloc iii. Free iv. realloc ๏‚ด Their performance varies in both execution time and required memory.
  • 4. Memory Allocation Local Variables Free Memory C program instructions Global Variables Stack Heap Permanent Storage Area
  • 5. Malloc ๏‚ด A block of memory is allocated by malloc ๏‚ด Allocates single block of requested memory. ๏‚ด It doesn't initialize memory at execution time, so it has garbage value initially. ๏‚ด Allocates specified number of bytes ๏‚ด Returns NULL if memory is not sufficient. ๏‚ด Syntax of malloc(): ๐’—๐’๐’Š๐’… โˆ— ๐’Ž๐’‚๐’๐’๐’๐’„ ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’ ; ๏‚ด Can be written as:- ๐’‘๐’•๐’“ = ๐’„๐’‚๐’”๐’• โˆ’ ๐’•๐’š๐’‘๐’† โˆ— ๐’Ž๐’‚๐’๐’๐’๐’„ ๐’ƒ๐’š๐’•๐’† โˆ’ ๐’”๐’Š๐’›๐’† ;
  • 6. Calloc ๏‚ด Calloc is normally used for requesting memory space at run time. ๏‚ด It stores data types such as arrays and structures. ๏‚ด Allocates multiple block of requested memory. ๏‚ด Calloc allocates memory to hold 30 records. ๏‚ด Allocates specified numbers of bytes. ๏‚ด Initially initialize all bytes to zero. ๏‚ด syntax of calloc(): ๐’—๐’๐’Š๐’… โˆ— ๐’„๐’‚๐’๐’๐’๐’„ ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’, ๐’”๐’Š๐’›๐’† โˆ’ ๐’• ๐’”๐’Š๐’›๐’† ; ๏‚ด Can be written as: ๐’‘๐’•๐’“ = ๐’„๐’‚๐’”๐’• โˆ’ ๐’•๐’š๐’‘๐’† โˆ— ๐’„๐’‚๐’๐’๐’๐’„ ๐’, ๐’†๐’๐’†๐’Ž โˆ’ ๐’”๐’Š๐’›๐’† ;
  • 7. Malloc() Calloc() ๏‚ด The name malloc stands for memory allocation. ๏‚ด Takes only one argument ๏‚ด Amount of memory is in bytes ๏‚ด Does not initialize the memory allocated ๏‚ด The name calloc stands for contiguous allocation ๏‚ด Takes two argument ๏‚ด Amount of memory of single variable in numbers of variable ๏‚ด Guarantees that all the bytes of allocated memory block have been initialized to 0 Difference between malloc & calloc
  • 8. Programs for malloc & calloc /* Allocate memory for an int. */ int *ptr = (int*) malloc(sizeof (int)); if (ptr == NULL) { printf("Could not allocate memoryn"); exit(-1); } else printf("Memory allocated successfully.n"); /* Allocating memory for an array of 10 elements of type int. */ int *ptr = (int*) calloc(10 ,sizeof (int)); if (ptr == NULL) { printf("Could not allocate memoryn"); exit(-1); } else printf("Memory allocated successfully.n");
  • 9. Reference ๏‚ด https://www.javatpoint.com/cpp-tutorial ๏‚ด https://www.studytonight.com/c/dynamic-memory- allocation-in-c.php ๏‚ด https://www.javatpoint.com/dynamic-memory-allocation-in-c ๏‚ด https://www.geeksforgeeks.org/calloc-versus-malloc/ ๏‚ด https://www.programiz.com/c-programming/c-dynamic- memory-allocation