SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
1D Array in Assembly Language
1D Array in Assembly Language1D Array in Assembly Language
1D Array in Assembly LanguageJaveria Yaqoob
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and UnionsAshim Lamichhane
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languageTanmay Modi
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statementSyed Zaid Irshad
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python TutorialSimplilearn
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 

Tendances (20)

Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Function
FunctionFunction
Function
 
Array in c++
Array in c++Array in c++
Array in c++
 
1D Array in Assembly Language
1D Array in Assembly Language1D Array in Assembly Language
1D Array in Assembly Language
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
File handling in c
File handling in cFile handling in c
File handling 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
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
File in C language
File in C languageFile in C language
File in C language
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Structure in C
Structure in CStructure in C
Structure in C
 

Similaire à Dynamic memory allocation

Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
 
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
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
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(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 allocationGaurav Mandal
 
Dma
DmaDma
DmaAcad
 

Similaire à Dynamic memory allocation (20)

Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
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
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic 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
 
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...
 
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
 

Plus de 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
 

Plus de 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
 

Dernier

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Dernier (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .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