Download - Intoduction to dynamic memory allocation
Title
~Contents~~Introduction to Dynamic Memory Allocation
~Static Memory Allocation~Dynamic Memory Allocation
~Maloc, Calloc & Realloc~~Malloc~Calloc
~Realloc~Malloc and Calloc Difference
Title Variables
rollNo
float percentage;int rollNo;
2 byte
percentage
4 byte
TitleArray Memory Allocation
Array Memory AllocationExample :-
int rollNo[10];
rollNo
20 byte
TitleStatic Memory Allocation
Static Memory Allocation
But there are some issues with static memory allocation..!
void main(){int rollNo[10], n, i;printf(“Number of students:”);scanf(“%d”, &n);for(i=0; i<n; i++){
scanf(“%d”, &rollNo[i] );}}
Output:Number of Students: 3101518
rollNo
10 15 18
Wastage of Memory
void main(){int rollNo[10], n, i;printf(“Number of students:”);scanf(“%d”, &n);for(i=0; i<n; i++){
scanf(“%d”, &rollNo[i] );}}
Output:Number of Students: 60
rollNo
0 1 2 3 4 5 6 7 8 9
Lack of Memory
TitleProblem with Static Memory Allocation
Static Memory Allocation
Can not change the size of allocated memory..!
TitleDynamic Memory Allocation
Dynamic Memory Allocation
malloccallocrealloc
free
Title malloc
malloc function is used to allocate memory blocks at run time.
Header File : #include<stdlib.h>Usage form : ptr = (ptr-type *) malloc(size-in-
byte);
Title Example : malloc
void main(){int rollNo[6];
int *ptr;ptr =
}
(int *)
malloc
(6)rollNo
12 byte
6 byte
byte
;
Title Example : malloc
void main(){int rollNo[6];
int *ptr;ptr =
}
(int *)
malloc
(12) ;
12 byte
Title Example : malloc
void main(){int rollNo[6];
int *ptr;ptr =
}
(int *)
malloc
(12) ;
100
102
104
106
108
110
ptr
100
Example : malloc
void main(){int i;int *ptr;ptr = (int *) malloc(12);for(i=0; i < 6 ; i++){
scanf(“%d”, ptr+i );}for(i=0; i<6; i++){
printf(“ %d ”, *(ptr+i) );}}
100
102
104
106
108
110
ptr
100
Output:
5 10 15 20 25 30
5 10 15 20 25 305 10 15 20 25 30
Example : malloc
void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n*2 );for(i=0;i<n;i++){
scanf(“%d”, ptr+i );}for(i=0;i<n;i++){
printf(“ %d ”, *(ptr+i) );}}
210
212
214
216
ptr
210
20 34 54 11
Output:
20 34 54 11
420 34 54 11
Title calloc
calloc function is used to allocate multiple memory blocks at run time.
Header File : #include<stdlib.h>Usage form : ptr = (ptr-type *) calloc(n,size);
Example : malloc
void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){
scanf(“%d”, ptr+i );}for(i=0;i<n;i++){
printf(“ %d ”, *(ptr+i) );}}
6 byte
n = 3
Example : malloc
void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) calloc( n , sizeof(int) );for(i=0;i<n;i++){
scanf(“%d”, ptr+i );}for(i=0;i<n;i++){
printf(“ %d ”, *(ptr+i) );}}
n = 3
2 byte
3
realloc function
int n, *ptr;scanf(“%d”, &n);
ptr = (int *) malloc( n* sizeof(int) );
int a[5];
10 byte
ptr
n = 5
What if user want to store only two values after entering n=5..?
10 byte
a
realloc function
int n, *ptr;scanf(“%d”, &n);
ptr = (int *) malloc( n* sizeof(int) );
int a[n];
4 byte
ptr
n = 5
reallocExample:
realloc (ptr, 4);
10 byte
a
realloc function
int n, *ptr;scanf(“%d”, &n);
ptr = (int *) malloc( n* sizeof(int) );
int a[n];
8 byte
ptr
n = 5
reallocExample:
realloc (ptr, 8);
10 byte
a
free function
void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){
scanf(“%d”, ptr+i );}for(i=0;i<n;i++){
printf(“ %d ”, *(ptr+i) );}}
10 byte
n = 5
freeExample:
free(ptr);
ptr
free function
void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){
scanf(“%d”, ptr+i );}for(i=0;i<n;i++){
printf(“ %d ”, *(ptr+i) );}}
n = 5
freeExample:
free(ptr);
ptr
TitleMalloc vs. CallocMalloc Calloc
The name malloc stands for memory allocation. The name calloc stands for contiguous allocation.
Allocates single block of storage space Allocates multiple blocks of storage
Syntax: void *malloc (size_in_bytes); Syntax: void *calloc (number_of_blocks, size_of_each_block_in_bytes);
Number of argument is 1 Number of arguments are 2
The contents of allocated memory are not changed. i.e., the memory contains unpredictable or garbage values. This presents a risk
The allocated region is initialized to zero
malloc is faster than calloc. calloc takes little longer than malloc because of the extra step of initializing the allocated memory by zero.
Title
Thank You
Do visit for more at www.codeitoff.blogspot.com