Dynamic
Memory
Allocation
Dynamic Memory Allocation (DMA)
• If memory is allocated at runtime (during execution of program) then it is called
dynamic memory.
• Memory can be accessed only through a pointer.
• Heap - The remainder of the dynamic storage area is commonly allocated to
the heap, from which application programs may dynamically allocate memory, as
required.
When DMA is needed?
• It is used when number of variables are not known in advance or large in size.
• Memory can be allocated at any time and can be released at any time during
runtime.
Dynamic Memory Allocation (DMA) 2
malloc() function
• malloc () is used to allocate a fixed amount of memory during the
execution of a program.
• malloc () allocates size_in_bytes of memory from heap, if the allocation
succeeds, a pointer to the block of memory is returned else NULL is returned.
• Allocated memory space may not be contiguous.
• Each block contains a size, a pointer to the next block, and the space itself.
• The blocks are kept in ascending order of storage address, and the last block
points to the first.
• The memory is not initialized.
Syntax Description
ptr_var = (cast_type *) This statement returns a pointer to size_in_bytes of uninitialized
malloc (size_in_bytes); storage, or NULL if the request cannot be satisfied.
Example: fp = (int *)malloc(sizeof(int) *20);
Dynamic Memory Allocation (DMA) 3
Write a C program to allocate memory using malloc.
Program
1 #include <stdio.h>
2 void main()
3 {
4 int *fp; //fp is a pointer variable
5 fp = (int *)malloc(sizeof(int)); //returns a pointer to int size storage
6 *fp = 25; //store 25 in the address pointed by fp
7 printf("%d", *fp); //print the value of fp, i.e. 25
8 free(fp); //free up the space pointed to by fp
9 }
Output
25
Dynamic Memory Allocation (DMA) 4
calloc() function
• calloc() is used to allocate a block of memory during the execution
of a program
• calloc() allocates a region of memory to hold no_of_blocks of
size_of_block each, if the allocation succeeds then a pointer to the
block of memory is returned else NULL is returned.
• The memory is initialized to ZERO.
Syntax Description
ptr_var = (cast_type *) This statement returns a pointer to no_of_blocks of size
calloc (no_of_blocks, size_of_blocks, it returns NULL if the request cannot be satisfied.
size_of_block);
Example:
int n = 20;
fp = (int *)calloc(n, sizeof(int));
Dynamic Memory Allocation (DMA) 5
Write a C program to allocate memory using calloc.
Program
1 #include <stdio.h>
2 void main()
3 {
4 int i, n; //i, n are integer variables
5 int *fp; //fp is a pointer variable
6 printf("Enter how many numbers: ");
7 scanf("%d", &n);
8 fp = (int *)calloc(n, sizeof(int)); //calloc returns a pointer to n blocks
9 for(i = 0; i < n; i++) //loop through until all the blocks are read
10 {
11 scanf("%d",fp); //read and store into location where fp points
12 fp++; //increment the pointer variable
13 }
14 free(fp); //frees the space pointed to by fp
}
Dynamic Memory Allocation (DMA) 6
malloc() and calloc() function
BASIS OF COMPARISON MALLOC() CALLOC()
Assigns single block of demanded memory. Assigns multiple blocks of the requested
No of blocks
memory.
Syntax void *malloc(size_t size); void *calloc(size_t num, size_t size);
malloc() doesn't clear and initialize the allocated The allocated memory is initialized to zero
Initialization
memory. by using calloc().
malloc() function allocates memory of size 'size' calloc() function allocates memory the size
Manner of Allocation
from the heap. of which is equal to num *size.
Speed Fast Comparatively slow.
Dynamic Memory Allocation (DMA) 7
realloc() function
• realloc() changes the size of the object pointed to by pointer fp to
specified size.
• The contents will be unchanged up to the minimum of the old and new
sizes.
• If the new size is larger, the new space will be uninitialized.
• realloc() returns a pointer to the new space, or NULL if the request cannot
be satisfied, in which case *fp is unchanged.
Syntax Description
ptr_var = (cast_type *) This statement returns a pointer to new space, or NULL if the request
realloc (void *fp, cannot be satisfied.
size_t);
Example: fp = (int *)realloc(fp,sizeof(int)*20);
Dynamic Memory Allocation (DMA) 8
Write a C program to allocate memory using realloc.
Program
1 #include <stdio.h>
2 void main()
3 {
4 int *fp; //fp is a file pointer
5 fp = (int *)malloc(sizeof(int)); //malloc returns a pointer to int size storage
6 *fp = 25; //store 25 in the address pointed by fp
7 fp =(int *)realloc(fp, 2*sizeof(int)); //returns a pointer to new space
8 printf("%d", *fp); //print the value of fp
9 free(fp); //free up the space pointed to by fp
}
Output
25
Dynamic Memory Allocation (DMA) 9
free() function
• free deallocates the space pointed to by fp.
• It does nothing if fp is NULL.
• fp must be a pointer to space previously allocated by calloc, malloc
or realloc.
Syntax Description
void free(void *); This statement free up the memory not needed anymore.
Example: free(fp);
Dynamic Memory Allocation (DMA) 10
Write a C program to sort numbers using malloc
Program Program (cont.)
1 #include<stdio.h> 17 if(p[i] > p[j])
2 #include<stdlib.h> 18 {
3 void main() 19 t = p[i];
4 { 20 p[i] = p[j];
5 int i,j,t,n; 21 p[j] = t;
6 int *p; 22 }
7 printf("Enter value of n: "); 23 }
8 scanf("%d", &n); 24 }
9 p=(int *) malloc(n * sizeof(int)); 25 printf("Ascending order\n");
10 printf("Enter values\n"); 26 for(i=0; i<n; i++)
11 for(i=0; i<n; i++) 27 printf("%d\n", p[i]);
12 scanf("%d", &p[i]); 28 free(p);
13 for(i=0; i<n; i++) 29 }
14 {
15 for(j= i+1; j<n; j++)
16 {
Dynamic Memory Allocation (DMA) 11
Write a C program to find square of numbers using calloc
Program Output
1 #include<stdio.h> Enter value of n: 3
2 #include<stdlib.h> Enter values
3 void main() 3
4 { 2
5 int i,n; 5
6 int *p; Square of 3 = 9
7 printf("Enter value of n: "); Square of 2 = 4
8 scanf("%d",&n); Square of 5 = 25
9 p=(int*)calloc(n,sizeof(int));
10 printf("Enter values\n");
11 for(i=0;i<n;i++)
12 scanf("%d",&p[i]);
13 for(i=0;i<n;i++)
14 printf("Square of %d = %d\n", p[i],
15 p[i] * p[i]);
16 free(p);
17 }
Dynamic Memory Allocation (DMA) 12
Write a C program to add/remove item from a list using realloc
Program Program (cont.)
1 #include<stdio.h> 18
2 #include<stdlib.h> 19 printf("Enter new size of list: ");
3 void main() 20 scanf("%d", &n2);
4 { 21
5 int i, n1, n2; 22 fp = realloc(fp, n2 * sizeof(int));
6 int *fp; 23 if(n2 > n1)
7 printf("Enter size of list: "); 24 {
8 scanf("%d", &n1); 25 printf("Enter %d numbers\n", n2 - n1);
9 fp=(int *) malloc (n1 * sizeof(int)); 26 for(i = n1; i < n2; i++)
10 27 scanf("%d", &fp[i]);
11 printf("Enter %d numbers\n", n1); 28 }
12 for(i = 0; i < n1; i++) 29 printf("The numbers in the list are\n");
13 scanf("%d", &fp[i]); 30 for(i = 0; i < n2; i++)
14 31 printf("%d\n", fp[i]);
15 printf("The numbers in the list are\n"); 32 }
16 for(i = 0; i < n1; i++)
17 printf("%d\n", fp[i]);
Dynamic Memory Allocation (DMA) 13
Write a C program to add/remove item from a list using realloc
Output
Dynamic Memory Allocation (DMA) 14
Practice Programs
1) Write a C program to calculate sum of n numbers entered by user.
2) Write a C program to input and print text using DMA
Dynamic Memory Allocation (DMA) 15
Thank you