SlideShare a Scribd company logo
1 of 14
Active Learning Assignment
Semester : 3rd
Branch : CE Engineering
Subject : Data Structures(2130702)
Topic : “Dynamic Memory Allocation”
Prepared by : Vaani Pathak
Enrollement No. :170120107131
Guided by : Prof. Ketan Pandya
1Gandhinagar Institute of Technology
Introduction to Dynamic Memory
Allocation
• Dynamic Allocation is by which a program can
obtain memory while it is running.
• Pointers provide necessary support for C’s
powerful dynamic memory allocation.
• In general global variables are allocated
storage at compile time. Local variables can
use the program stack.
Dynamic Allocation Functions
• 1) malloc()
• 2)free()
• 3)calloc()
Malloc()
• It dynamically allocates memory during the
program.
• Prototype of this function is
Void*malloc(size_t, number_of_bytes).
After a successful call, malloc() returns a pointer
to the first byte of the region of memory.
Example of Malloc()
• Example:-
Code fragments below allocate 1000 bytes of
contiguous memory.
char*p;
p=(char*)malloc(1000);
Here,(char*)forces void pointer to become a
character
Program For Malloc()
#include<stdio.h>
#include<conio.h>
Void main()
{
int n, I, *p, sum=0;
printf(“enter elements: ”)
Scanf(“%f”, &n);
printf(“using malloc n:”);
p=(int*)malloc(n*size of (int));
if(p==NULL)
{
printf(“Error”);
}
printf(“enter elements of arrays:”);
For(i=0,i<n,i++)
…
{
scanf(“%d”, p+i);
sum+=*(p+i);
}
printf(“sum=%d n”, sum);
free(p);
}
Free() Function
• It is the opposite of malloc().
• It returns previously allocated memory to the
system.
• Prototype: void free(void*p);
• It is critical that you never call free() with an
invalid argument. That will delete the free list.
Calloc() function
• Calloc() function allocates an amount of
memory equal to num*size.
• It allocates enough memory for an array of
num objects and size bytes long.
• Memory allocated by calloc() is released by
free() function.
• Prototype:
• void*calloc(size_t,n,size_t,number_of_bytes);
Example of Calloc()
• Example of Calloc()
• Code fragments below dynamically allocates 100
elements to an integer array.
Int*p;
p=(int*)calloc(100,sizeof(int));
(int*) forces the void pointer to become a integer
Program for Calloc()#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Void main()
{
int, I,n, *ptr, sum=0;
clrscr();
printf(“enter any number:”);
scanf(“%d”, &n);
ptr=(int*)calloc(n size of (int));
if)ptr==null);
{
printf(“!Error”);
exit(o);
}
printf(“enter elements of array:”);
for(i=o;i<n;++i)
{
scanf(“%d”, ptr +1);
sum+=*(ptr+1);
}printf(“sum=%d”,sum);
free(ptr);
Getch();
}
Difference between Malloc() and
Calloc()
Malloc()
• In Malloc, how much memory we want we can
pass it as one slot.
• Malloc is faster than calloc.
• Malloc dosnt initializes the allocated
memory.It contains garbage values.
…
Calloc()
• We need to split ad pass the memory as we
want.
• Calloc is slower than malloc.
• Calloc initialize the allocated memory to zero.
• Number of arguments is 2.
THANK YOU

More Related Content

What's hot (20)

Programed I/O Modul..
Programed I/O Modul..Programed I/O Modul..
Programed I/O Modul..
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
File in C language
File in C languageFile in C language
File in C language
 
8251 USART
8251 USART8251 USART
8251 USART
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Array within a class
Array within a classArray within a class
Array within a class
 
Instruction set of 8085
Instruction set of 8085Instruction set of 8085
Instruction set of 8085
 
C Pointers
C PointersC Pointers
C Pointers
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
Console i/o for c++
Console i/o for c++Console i/o for c++
Console i/o for c++
 
Structure in C
Structure in CStructure in C
Structure in C
 

Similar to Dynamic Memory Allocation

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationViji B
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation웅식 전
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CPShubham Sinha
 
dynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxdynamicmemoryallocation.pptx
dynamicmemoryallocation.pptxNiharika606186
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxSKUP1
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationUTTAM VERMA
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
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).pptxhelpme43
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxkrishna50blogging
 

Similar to Dynamic Memory Allocation (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Dma
DmaDma
Dma
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
 
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
 
dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
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
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
Clanguage
ClanguageClanguage
Clanguage
 

More from vaani pathak

D’Alembert’s Solution of the Wave Equation
D’Alembert’s Solution of the Wave EquationD’Alembert’s Solution of the Wave Equation
D’Alembert’s Solution of the Wave Equationvaani pathak
 
file management functions
file management functionsfile management functions
file management functionsvaani pathak
 
interpolation of unequal intervals
interpolation of unequal intervalsinterpolation of unequal intervals
interpolation of unequal intervalsvaani pathak
 
Arc Length, Curvature and Torsion
Arc Length, Curvature and TorsionArc Length, Curvature and Torsion
Arc Length, Curvature and Torsionvaani pathak
 
Fixtures and Reflectors
Fixtures and ReflectorsFixtures and Reflectors
Fixtures and Reflectorsvaani pathak
 
Calculus ppt format
Calculus ppt formatCalculus ppt format
Calculus ppt formatvaani pathak
 
Planning for presentation
Planning for presentationPlanning for presentation
Planning for presentationvaani pathak
 
Types of polarisation
Types of polarisationTypes of polarisation
Types of polarisationvaani pathak
 
Air Pollution Acts
Air Pollution ActsAir Pollution Acts
Air Pollution Actsvaani pathak
 

More from vaani pathak (15)

D’Alembert’s Solution of the Wave Equation
D’Alembert’s Solution of the Wave EquationD’Alembert’s Solution of the Wave Equation
D’Alembert’s Solution of the Wave Equation
 
file management functions
file management functionsfile management functions
file management functions
 
error detection
error detectionerror detection
error detection
 
interpolation of unequal intervals
interpolation of unequal intervalsinterpolation of unequal intervals
interpolation of unequal intervals
 
Arc Length, Curvature and Torsion
Arc Length, Curvature and TorsionArc Length, Curvature and Torsion
Arc Length, Curvature and Torsion
 
Cpd 4.2
Cpd 4.2Cpd 4.2
Cpd 4.2
 
CPD ch 7
CPD ch 7CPD ch 7
CPD ch 7
 
Chain Drive
Chain DriveChain Drive
Chain Drive
 
AM FM PM
AM FM PMAM FM PM
AM FM PM
 
Fixtures and Reflectors
Fixtures and ReflectorsFixtures and Reflectors
Fixtures and Reflectors
 
Calculus ppt format
Calculus ppt formatCalculus ppt format
Calculus ppt format
 
Planning for presentation
Planning for presentationPlanning for presentation
Planning for presentation
 
Types of polarisation
Types of polarisationTypes of polarisation
Types of polarisation
 
CPU Algorithm
CPU Algorithm CPU Algorithm
CPU Algorithm
 
Air Pollution Acts
Air Pollution ActsAir Pollution Acts
Air Pollution Acts
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

Dynamic Memory Allocation

  • 1. Active Learning Assignment Semester : 3rd Branch : CE Engineering Subject : Data Structures(2130702) Topic : “Dynamic Memory Allocation” Prepared by : Vaani Pathak Enrollement No. :170120107131 Guided by : Prof. Ketan Pandya 1Gandhinagar Institute of Technology
  • 2. Introduction to Dynamic Memory Allocation • Dynamic Allocation is by which a program can obtain memory while it is running. • Pointers provide necessary support for C’s powerful dynamic memory allocation. • In general global variables are allocated storage at compile time. Local variables can use the program stack.
  • 3. Dynamic Allocation Functions • 1) malloc() • 2)free() • 3)calloc()
  • 4. Malloc() • It dynamically allocates memory during the program. • Prototype of this function is Void*malloc(size_t, number_of_bytes). After a successful call, malloc() returns a pointer to the first byte of the region of memory.
  • 5. Example of Malloc() • Example:- Code fragments below allocate 1000 bytes of contiguous memory. char*p; p=(char*)malloc(1000); Here,(char*)forces void pointer to become a character
  • 6. Program For Malloc() #include<stdio.h> #include<conio.h> Void main() { int n, I, *p, sum=0; printf(“enter elements: ”) Scanf(“%f”, &n); printf(“using malloc n:”); p=(int*)malloc(n*size of (int)); if(p==NULL) { printf(“Error”); } printf(“enter elements of arrays:”); For(i=0,i<n,i++)
  • 8. Free() Function • It is the opposite of malloc(). • It returns previously allocated memory to the system. • Prototype: void free(void*p); • It is critical that you never call free() with an invalid argument. That will delete the free list.
  • 9. Calloc() function • Calloc() function allocates an amount of memory equal to num*size. • It allocates enough memory for an array of num objects and size bytes long. • Memory allocated by calloc() is released by free() function. • Prototype: • void*calloc(size_t,n,size_t,number_of_bytes);
  • 10. Example of Calloc() • Example of Calloc() • Code fragments below dynamically allocates 100 elements to an integer array. Int*p; p=(int*)calloc(100,sizeof(int)); (int*) forces the void pointer to become a integer
  • 11. Program for Calloc()#include<stdio.h> #include<conio.h> #include<stdlib.h> Void main() { int, I,n, *ptr, sum=0; clrscr(); printf(“enter any number:”); scanf(“%d”, &n); ptr=(int*)calloc(n size of (int)); if)ptr==null); { printf(“!Error”); exit(o); } printf(“enter elements of array:”); for(i=o;i<n;++i) { scanf(“%d”, ptr +1); sum+=*(ptr+1); }printf(“sum=%d”,sum); free(ptr); Getch(); }
  • 12. Difference between Malloc() and Calloc() Malloc() • In Malloc, how much memory we want we can pass it as one slot. • Malloc is faster than calloc. • Malloc dosnt initializes the allocated memory.It contains garbage values.
  • 13. … Calloc() • We need to split ad pass the memory as we want. • Calloc is slower than malloc. • Calloc initialize the allocated memory to zero. • Number of arguments is 2.