SlideShare a Scribd company logo
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

PPTX
Dynamic memory allocation
PPTX
Dynamic Memory allocation
PPTX
Dynamic Memory Allocation(DMA)
PPTX
C dynamic ppt
PPTX
Dynamic memory allocation
PPTX
Dynamic memory allocation
PPTX
Dynamic Memory Allocation in C
PDF
Architecture OF 8085
Dynamic memory allocation
Dynamic Memory allocation
Dynamic Memory Allocation(DMA)
C dynamic ppt
Dynamic memory allocation
Dynamic memory allocation
Dynamic Memory Allocation in C
Architecture OF 8085

What's hot (20)

PPTX
Functions in c language
PPTX
Oop c++class(final).ppt
PPTX
Exception handling c++
PPT
File handling
PPTX
Dynamic memory allocation
PPTX
Presentation on pointer.
PPT
Array in c
PPT
Memory allocation in c
PPTX
Malloc() and calloc() in c
PPTX
Dynamic memory allocation in c++
PPTX
C Programming: Structure and Union
PPT
Unix ch03-03
PPT
Java static keyword
PPTX
Pointer in C++
PDF
Python - object oriented
PPTX
Storage classes in C
PDF
Break, Continue and Pass in Python.pdf
PPT
Primitive data types in java
PPTX
Function in c
PPT
working file handling in cpp overview
Functions in c language
Oop c++class(final).ppt
Exception handling c++
File handling
Dynamic memory allocation
Presentation on pointer.
Array in c
Memory allocation in c
Malloc() and calloc() in c
Dynamic memory allocation in c++
C Programming: Structure and Union
Unix ch03-03
Java static keyword
Pointer in C++
Python - object oriented
Storage classes in C
Break, Continue and Pass in Python.pdf
Primitive data types in java
Function in c
working file handling in cpp overview
Ad

Similar to Dynamic Memory Allocation (20)

PPTX
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PDF
Data Structure - Dynamic Memory Allocation
PDF
Programming in C language specially allocation
PDF
dynamic-allocation.pdf
PPSX
4 dynamic memory allocation
PDF
13. dynamic allocation
PPTX
Memory management CP
PPTX
16 dynamic-memory-allocation
PPTX
Dynamic memory allocation in c
PPTX
Dynamic Memory Allocation.pptx for c language and basic knowledge.
PPTX
dynamicmemoryallocation.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
pointers in c programming - example programs
PPTX
dynamic_v1-3.pptx
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PPT
Dynamic allocation
PDF
Memory Management for C and C++ _ language
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Data Structure - Dynamic Memory Allocation
Programming in C language specially allocation
dynamic-allocation.pdf
4 dynamic memory allocation
13. dynamic allocation
Memory management CP
16 dynamic-memory-allocation
Dynamic memory allocation in c
Dynamic Memory Allocation.pptx for c language and basic knowledge.
dynamicmemoryallocation.pptx
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
pointers in c programming - example programs
dynamic_v1-3.pptx
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
Dynamic allocation
Memory Management for C and C++ _ language
Ad

More from vaani pathak (15)

PPTX
D’Alembert’s Solution of the Wave Equation
PPTX
file management functions
PPTX
error detection
PPTX
interpolation of unequal intervals
PPTX
Arc Length, Curvature and Torsion
PPTX
Cpd 4.2
PPTX
CPD ch 7
PPTX
Chain Drive
PPTX
AM FM PM
PPT
Fixtures and Reflectors
PPT
Calculus ppt format
PPTX
Planning for presentation
PPTX
Types of polarisation
PPTX
CPU Algorithm
PPTX
Air Pollution Acts
D’Alembert’s Solution of the Wave Equation
file management functions
error detection
interpolation of unequal intervals
Arc Length, Curvature and Torsion
Cpd 4.2
CPD ch 7
Chain Drive
AM FM PM
Fixtures and Reflectors
Calculus ppt format
Planning for presentation
Types of polarisation
CPU Algorithm
Air Pollution Acts

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Well-logging-methods_new................
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Automation-in-Manufacturing-Chapter-Introduction.pdf
OOP with Java - Java Introduction (Basics)
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Internet of Things (IOT) - A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
573137875-Attendance-Management-System-original
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Model Code of Practice - Construction Work - 21102022 .pdf
web development for engineering and engineering
composite construction of structures.pdf
additive manufacturing of ss316l using mig welding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

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.