SlideShare a Scribd company logo
Dynamic Memory Allocation
Basic Idea
• Many a time we face situations where data is dynamic in nature.
• – Amount of data cannot be predicted beforehand.
• – Number of data items keeps changing during program execution.
• Such situations can be handled more easily and effectively using
dynamic memory management techniques.
• C language requires the number of elements in an array to be
specified at compile time.
• – Often leads to wastage of memory space or program failure.
• Dynamic Memory Allocation
• – Memory space required can be specified at the time of execution.
• – C supports allocating and freeing memory dynamically using library
routines.
Memory Allocation Process in C
• Memory allocated to any program is either from Stack or Heap.
• Stack memory is allocated to local variable, function call parameters etc.
• Heap memory is allocated to global variables or the variables which request
for memory space and asking for memory space is called dynamic allocation
of memory.
• Four basic functions which belong to stdlib.h or malloc.h or alloc.h for
dynamic memory allocation –
1. malloc()
2. calloc()
3. realloc()
4. free()
Example
void main(){
int i,N;
float *height;
float sum=0,avg;
printf("Input no. of studentsn");
scanf("%d", &N);
height = (float *) malloc(N * sizeof(float));
printf("Input heights for %d students n",N);
for (i=0; i<N; i++)
scanf ("%f", &height[i]);
for(i=0;i<N;i++)
sum += height[i];
avg = sum / (float) N;
printf("Average height = %f n“,avg);
free (height);
}
Calloc()
• Calloc function allocates the block of bytes from
heap.
• Syntax: void * calloc (num, size)
• This function takes two arguments that specify
the number of elements to be reserved, and the
size of each element in bytes
• It allocates memory block equivalent to num*
size .
Example2: calloc and malloc
Realloc Example
void main() {
int* array,*array1;
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
array = (int*) calloc(n,sizeof(int));
for (i=0; i<n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &array[i]);
}
array1 = (int*) realloc(array,(n+1)*sizeof(int));
if(array1)
printf("Memory Allocated");
printf("nThe Dynamic Array is: n");
for (i=0; i<(n+1); i++)
printf("The value of %d is %dn", i, array1[i]);
}
O/P?
#include <malloc.h>
#include <stdio.h>
void main() {
int* ptr;
ptr=(int *)malloc(sizeof(int));
*ptr=5;
printf("%d ",*ptr);
free(ptr);
*ptr=7;
printf("%d ",*ptr);
}

More Related Content

What's hot

Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
Omair Imtiaz Ansari
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
kash95
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Union In language C
Union In language CUnion In language C
Union In language C
Ravi Singh
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
Grishma Rajput
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
vaani pathak
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Burhanuddin Kapadia
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
Arpana shree
 
Pointers
PointersPointers
Pointers
sarith divakar
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 

What's hot (20)

Structure in C
Structure in CStructure in C
Structure in C
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Union In language C
Union In language CUnion In language C
Union In language C
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Pointers
PointersPointers
Pointers
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Similar to 16 dynamic-memory-allocation

Memory management CP
Memory management  CPMemory management  CP
Memory management CP
Shubham Sinha
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
UTTAM VERMA
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
Niharika606186
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Dma
DmaDma
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
ngonidzashemutsipa
 
DIY Java Profiling
DIY Java ProfilingDIY Java Profiling
DIY Java Profiling
Roman Elizarov
 
Introduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptxIntroduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptx
poongothai11
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
Amit Kundu
 
Stack and heap
Stack and heapStack and heap
C language
C languageC language
C language
Mukul Kirti 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 PPT
AkhilMishra50
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
SURBHI SAROHA
 
Functions using stack and heap
Functions using stack and heapFunctions using stack and heap
Functions using stack and heap
baabtra.com - No. 1 supplier of quality freshers
 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
Simplilearn
 
Storage class
Storage classStorage class
Storage class
MANJULA_AP
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Muhammed Thanveer M
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
Frijo Francis
 
Presentation on dynamic memory management
Presentation on dynamic memory managementPresentation on dynamic memory management
Presentation on dynamic memory management
Bhimsen Joshi
 

Similar to 16 dynamic-memory-allocation (20)

Memory management CP
Memory management  CPMemory management  CP
Memory management CP
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptx
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Dma
DmaDma
Dma
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
DIY Java Profiling
DIY Java ProfilingDIY Java Profiling
DIY Java Profiling
 
Introduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptxIntroduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptx
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
C language
C languageC language
C language
 
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
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Functions using stack and heap
Functions using stack and heapFunctions using stack and heap
Functions using stack and heap
 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
 
Storage class
Storage classStorage class
Storage class
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
Presentation on dynamic memory management
Presentation on dynamic memory managementPresentation on dynamic memory management
Presentation on dynamic memory management
 

More from Rohit Shrivastava

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
Rohit Shrivastava
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
14 strings
14 strings14 strings
14 strings
Rohit Shrivastava
 
11 functions
11 functions11 functions
11 functions
Rohit Shrivastava
 
10 array
10 array10 array
8 number-system
8 number-system8 number-system
8 number-system
Rohit Shrivastava
 
7 decision-control
7 decision-control7 decision-control
7 decision-control
Rohit Shrivastava
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
Rohit Shrivastava
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
Rohit Shrivastava
 
2 memory-and-io-devices
2 memory-and-io-devices2 memory-and-io-devices
2 memory-and-io-devices
Rohit Shrivastava
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
Rohit Shrivastava
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 

More from Rohit Shrivastava (13)

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
14 strings
14 strings14 strings
14 strings
 
11 functions
11 functions11 functions
11 functions
 
10 array
10 array10 array
10 array
 
8 number-system
8 number-system8 number-system
8 number-system
 
7 decision-control
7 decision-control7 decision-control
7 decision-control
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
2 memory-and-io-devices
2 memory-and-io-devices2 memory-and-io-devices
2 memory-and-io-devices
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 

Recently uploaded

Here's Why Every Semi-Truck Should Have ELDs
Here's Why Every Semi-Truck Should Have ELDsHere's Why Every Semi-Truck Should Have ELDs
Here's Why Every Semi-Truck Should Have ELDs
jennifermiller8137
 
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
78tq3hi2
 
Catalytic Converter theft prevention - NYC.pptx
Catalytic Converter theft prevention - NYC.pptxCatalytic Converter theft prevention - NYC.pptx
Catalytic Converter theft prevention - NYC.pptx
Blue Star Brothers
 
EV Charging at Multifamily Properties by Kevin Donnelly
EV Charging at Multifamily Properties by Kevin DonnellyEV Charging at Multifamily Properties by Kevin Donnelly
EV Charging at Multifamily Properties by Kevin Donnelly
Forth
 
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
MarynaYurchenko2
 
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
78tq3hi2
 
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
afkxen
 
Charging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
Charging and Fueling Infrastructure Grant: Round 2 by Brandt HertensteinCharging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
Charging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
Forth
 
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
afkxen
 
Charging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
Charging Fueling & Infrastructure (CFI) Program Resources by Cat PleinCharging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
Charging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
Forth
 
Charging Fueling & Infrastructure (CFI) Program by Kevin Miller
Charging Fueling & Infrastructure (CFI) Program  by Kevin MillerCharging Fueling & Infrastructure (CFI) Program  by Kevin Miller
Charging Fueling & Infrastructure (CFI) Program by Kevin Miller
Forth
 
Dahua Security Camera System Guide esetia
Dahua Security Camera System Guide esetiaDahua Security Camera System Guide esetia
Dahua Security Camera System Guide esetia
Esentia Systems
 
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
g1inbfro
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
u2cz10zq
 
Expanding Access to Affordable At-Home EV Charging by Vanessa Warheit
Expanding Access to Affordable At-Home EV Charging by Vanessa WarheitExpanding Access to Affordable At-Home EV Charging by Vanessa Warheit
Expanding Access to Affordable At-Home EV Charging by Vanessa Warheit
Forth
 
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
utuvvas
 
EV Charging at MFH Properties by Whitaker Jamieson
EV Charging at MFH Properties by Whitaker JamiesonEV Charging at MFH Properties by Whitaker Jamieson
EV Charging at MFH Properties by Whitaker Jamieson
Forth
 
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
ggany
 

Recently uploaded (18)

Here's Why Every Semi-Truck Should Have ELDs
Here's Why Every Semi-Truck Should Have ELDsHere's Why Every Semi-Truck Should Have ELDs
Here's Why Every Semi-Truck Should Have ELDs
 
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
快速办理(napier毕业证书)英国龙比亚大学毕业证在读证明一模一样
 
Catalytic Converter theft prevention - NYC.pptx
Catalytic Converter theft prevention - NYC.pptxCatalytic Converter theft prevention - NYC.pptx
Catalytic Converter theft prevention - NYC.pptx
 
EV Charging at Multifamily Properties by Kevin Donnelly
EV Charging at Multifamily Properties by Kevin DonnellyEV Charging at Multifamily Properties by Kevin Donnelly
EV Charging at Multifamily Properties by Kevin Donnelly
 
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
53286592-Global-Entrepreneurship-and-the-Successful-Growth-Strategies-of-Earl...
 
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
原版制作(Exeter毕业证书)埃克塞特大学毕业证完成信一模一样
 
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
一比一原版(WashU文凭证书)圣路易斯华盛顿大学毕业证如何办理
 
Charging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
Charging and Fueling Infrastructure Grant: Round 2 by Brandt HertensteinCharging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
Charging and Fueling Infrastructure Grant: Round 2 by Brandt Hertenstein
 
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
一比一原版(Columbia文凭证书)哥伦比亚大学毕业证如何办理
 
Charging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
Charging Fueling & Infrastructure (CFI) Program Resources by Cat PleinCharging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
Charging Fueling & Infrastructure (CFI) Program Resources by Cat Plein
 
Charging Fueling & Infrastructure (CFI) Program by Kevin Miller
Charging Fueling & Infrastructure (CFI) Program  by Kevin MillerCharging Fueling & Infrastructure (CFI) Program  by Kevin Miller
Charging Fueling & Infrastructure (CFI) Program by Kevin Miller
 
Dahua Security Camera System Guide esetia
Dahua Security Camera System Guide esetiaDahua Security Camera System Guide esetia
Dahua Security Camera System Guide esetia
 
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
原版制作(澳洲WSU毕业证书)西悉尼大学毕业证文凭证书一模一样
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
 
Expanding Access to Affordable At-Home EV Charging by Vanessa Warheit
Expanding Access to Affordable At-Home EV Charging by Vanessa WarheitExpanding Access to Affordable At-Home EV Charging by Vanessa Warheit
Expanding Access to Affordable At-Home EV Charging by Vanessa Warheit
 
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
原版定做(mmu学位证书)英国曼彻斯特城市大学毕业证本科文凭原版一模一样
 
EV Charging at MFH Properties by Whitaker Jamieson
EV Charging at MFH Properties by Whitaker JamiesonEV Charging at MFH Properties by Whitaker Jamieson
EV Charging at MFH Properties by Whitaker Jamieson
 
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
按照学校原版(UniSA文凭证书)南澳大学毕业证快速办理
 

16 dynamic-memory-allocation

  • 2. Basic Idea • Many a time we face situations where data is dynamic in nature. • – Amount of data cannot be predicted beforehand. • – Number of data items keeps changing during program execution. • Such situations can be handled more easily and effectively using dynamic memory management techniques. • C language requires the number of elements in an array to be specified at compile time. • – Often leads to wastage of memory space or program failure. • Dynamic Memory Allocation • – Memory space required can be specified at the time of execution. • – C supports allocating and freeing memory dynamically using library routines.
  • 3. Memory Allocation Process in C • Memory allocated to any program is either from Stack or Heap. • Stack memory is allocated to local variable, function call parameters etc. • Heap memory is allocated to global variables or the variables which request for memory space and asking for memory space is called dynamic allocation of memory. • Four basic functions which belong to stdlib.h or malloc.h or alloc.h for dynamic memory allocation – 1. malloc() 2. calloc() 3. realloc() 4. free()
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. Example void main(){ int i,N; float *height; float sum=0,avg; printf("Input no. of studentsn"); scanf("%d", &N); height = (float *) malloc(N * sizeof(float)); printf("Input heights for %d students n",N); for (i=0; i<N; i++) scanf ("%f", &height[i]); for(i=0;i<N;i++) sum += height[i]; avg = sum / (float) N; printf("Average height = %f n“,avg); free (height); }
  • 10.
  • 11. Calloc() • Calloc function allocates the block of bytes from heap. • Syntax: void * calloc (num, size) • This function takes two arguments that specify the number of elements to be reserved, and the size of each element in bytes • It allocates memory block equivalent to num* size .
  • 13.
  • 14.
  • 15. Realloc Example void main() { int* array,*array1; int n, i; printf("Enter the number of elements: "); scanf("%d", &n); array = (int*) calloc(n,sizeof(int)); for (i=0; i<n; i++) { printf("Enter number %d: ", i); scanf("%d", &array[i]); } array1 = (int*) realloc(array,(n+1)*sizeof(int)); if(array1) printf("Memory Allocated"); printf("nThe Dynamic Array is: n"); for (i=0; i<(n+1); i++) printf("The value of %d is %dn", i, array1[i]); }
  • 16. O/P? #include <malloc.h> #include <stdio.h> void main() { int* ptr; ptr=(int *)malloc(sizeof(int)); *ptr=5; printf("%d ",*ptr); free(ptr); *ptr=7; printf("%d ",*ptr); }