DMA ch9
DMA ch9
MEMORY ALLOCATION
Dynamic Memory Allocation (DMA)
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
6 size storage
7 *fp = 25; //store 25 in the address pointed by fp
8 printf("%d", *fp); //print the value of fp, i.e. 25
9 free(fp); //free up the space pointed to by fp
}
Output
25
calloc() function
Syntax Description
ptr_var = (cast_typ This statement returns a pointer to no_of_blocks of size
e *) calloc size_of_blocks, it returns NULL if the request cannot be
(no_of_blocks, satisfied.
size_of_block);
Example:
int n = 20;
fp = (int *)calloc(n, sizeof(int));
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
9 to n blocks
10 for(i = 0; i < n; i++)
11 //loop through until all the blocks are
12 read
13 {
14 scanf("%d",fp); //read and store into location where fp poi
nts
fp++; //increment the pointer variable
}
free(fp); //frees the space pointed to by fp
realloc() function
Syntax Description
ptr_var = (cast_t This statement returns a pointer to new space, or
ype *) realloc NULL if the request cannot be satisfied.
(void *fp,
size_t); Example:
fp = (int *)realloc(fp,sizeof(int)*20);
Write a C program to allocate memory using realloc.
Program
1 #include <stdio.h>
void main()
2 {
3 int *fp; //fp is a file pointer
4 fp = (int *)malloc(sizeof(int)); //malloc returns a pointer to int size storage
5 *fp = 25; //store 25 in the address pointed by fp
fp =(int *)realloc(fp, 2*sizeof(int)); //returns a pointer to new space
6 printf("%d", *fp); //print the value of fp
7 free(fp); //free up the space pointed to by fp
8 }
9
Output
25
free() function
Syntax Description
void free(void *); This statement free up the memory not needed anymore.
Example: free(fp);
Write a C program to sort numbers using malloc
Output
Program
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 }
Write a C program to add/remove item from a list using realloc
Program
1 #include<stdio.h>
2 #include<stdlib.h>
3 void main()
4 {
5 int i, n1, n2;
6 int *fp;
7 printf("Enter size of list: ");
8 scanf("%d", &n1);
9 fp=(int *) malloc (n1 * sizeof(int));
10
11 printf("Enter %d numbers\n", n1);
12 for(i = 0; i < n1; i++)
13 scanf("%d", &fp[i]);
14
15 printf("The numbers in the list are\n");
16 for(i = 0; i < n1; i++)
17 printf("%d\n", fp[i]);
Program (cont.)
18
19 printf("Enter new size of list: ");
20 scanf("%d", &n2);
21
22 fp = realloc(fp, n2 * sizeof(int));
23 if(n2 > n1)
24 {
25 printf("Enter %d numbers\n", n2 - n1);
26 for(i = n1; i < n2; i++)
27 scanf("%d", &fp[i]);
28 }
29 printf("The numbers in the list are\n");
30 for(i = 0; i < n2; i++)
31 printf("%d\n", fp[i]);
32 }
Thank you !!