1. DEPARTMENT OF COMPUTER SCIENCE &
ENGG.
Presented By: Bhimsen D Joshi
Lecturer/CSE
B.L.D.E.A’S S.S.M. POLYTECHNIC
VIJAYAPUR
B.L.D.E.A’S S.S.M
POLYTECHNIC VIJAYAPUR
SEMINAR ON DYNAMIC
MEMORY MANAGEMENT
2. INTRODUCTION
The process of allocating memory at runtime is
known as dynamic memory allocation.
Dynamic memory allocation can be made using 4 functions
of stdlib.h header file.
malloc()
calloc()
realloc()
free()
3. Static Memory Allocation Dynamic Memory Allocation
Memory is allocated during
compile time.
Memory is allocated at run time.
Memory cannot be increased at
program execution time.
Memory can be increased at the
program execution time.
Array uses static allocation. Linked list uses dynamic allocation.
Dynamic memory allocation functions listed below.
Function Name Syntax
malloc() malloc (number *sizeof(int));
calloc() calloc (number, sizeof(int));
realloc() realloc (pointer_name, number *
sizeof(int));
free() free (pointer_name);
4. Function Name Description
malloc() allocates single block of memory.
calloc() allocates multiple block of memory.
realloc() reallocates the memory allocated by
malloc() or calloc() functions.
free() frees the allocated memory.
int *p;
p = (int*)malloc(1 * sizeof(int)); //memory space allocated to variable p
int *r;
r= (int*)calloc(1,2); //memory space allocated to variable r
int *p;
p = (int*)malloc(5 * sizeof(int));
p = (int*)reallc(p,10); //allocated a new memory to variable p
free(p);
SHORT EXAMPLES
5. #include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int *p,*r;
clrscr();
p = (int*)malloc(1*sizeof(int));
/* allocates memory block for storing an integer and block starting address store in a pointer p*/
*p=20; /* The value 20 stored in a block */
printf("nThe Value Stored in p is %d",*p);
r = (int*)calloc(1,2);
/* allocates memory block for storing an integer and block starting address store in a pointer r */
*r=40; /* The value 40 stored in a block */
printf("nThe Value Stored in r is %d",*r);
p = (int*)realloc(p,2); /* reallocates memory blocks for storing 2 integer values */
p[1]=25;
printf(“nP Block values are %d %d",p[0],p[1]);
free(p);
getch();
}
A COMPLETE EXAMPLE TO SHOW MEMORY ALLOCATION
FUNCTIONS.
4000 5000
20
40
20 25
p r
p
0 1
4000 4002
4000
2000 3000
5000
4000