Dynamic Memory Allocation
{C}
Programming
Programming for Problem Solving (PPS)
GTU # 3110003
USING
Prof. Nilesh Gambhava
�Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot
Dynamic Memory Allocation (DMA)
When DMA is needed?
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
2
malloc() function
Syntax | Description |
ptr_var = (cast_type *) malloc (size_in_bytes); | This statement returns a pointer to size_in_bytes of uninitialized storage, or NULL if the request cannot be satisfied. Example: fp = (int *)malloc(sizeof(int) *20); |
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
3
Write a C program to allocate memory using malloc.
#include <stdio.h>
void main()
{
int *fp; //fp is a pointer variable
fp = (int *)malloc(sizeof(int)); //returns a pointer to int size storage
*fp = 25; //store 25 in the address pointed by fp
printf("%d", *fp); //print the value of fp, i.e. 25
free(fp); //free up the space pointed to by fp
}
�
1
2
3
4
5
6
7
8
9
Program
Output
25
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
4
calloc() function
Syntax | Description |
ptr_var = (cast_type *) calloc (no_of_blocks, size_of_block); | This statement returns a pointer to no_of_blocks of size size_of_blocks, it returns NULL if the request cannot be satisfied. Example: int n = 20; fp = (int *)calloc(n, sizeof(int)); |
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
5
Write a C program to allocate memory using calloc.
#include <stdio.h>
void main()
{
int i, n; //i, n are integer variables
int *fp; //fp is a pointer variable
printf("Enter how many numbers: ");
scanf("%d", &n);
fp = (int *)calloc(n, sizeof(int)); //calloc returns a pointer to n blocks
for(i = 0; i < n; i++) //loop through until all the blocks are read
{
scanf("%d",fp); //read and store into location where fp points
fp++; //increment the pointer variable
}
free(fp); //frees the space pointed to by fp
}
�
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Program
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
6
realloc() function
Syntax | Description |
ptr_var = (cast_type *) realloc (void *fp, size_t); | This statement returns a pointer to new space, or NULL if the request cannot be satisfied. Example: fp = (int *)realloc(fp,sizeof(int)*20); |
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
7
Write a C program to allocate memory using realloc.
#include <stdio.h>
void main()
{
int *fp; //fp is a file pointer
fp = (int *)malloc(sizeof(int)); //malloc returns a pointer to int size storage
*fp = 25; //store 25 in the address pointed by fp
fp =(int *)realloc(fp, 2*sizeof(int)); //returns a pointer to new space
printf("%d", *fp); //print the value of fp
free(fp); //free up the space pointed to by fp
}
1
2
3
4
5
6
7
8
9
Program
Output
25
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
8
free() function
Syntax | Description |
void free(void *); | This statement free up the memory not needed anymore. Example: free(fp); |
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
9
Write a C program to sort numbers using malloc
if(p[i] > p[j])
{
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
printf("Ascending order\n");
for(i=0; i<n; i++)
printf("%d\n", p[i]);
free(p);
}
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,t,n;
int *p;
printf("Enter value of n: ");
scanf("%d", &n);
p=(int *) malloc(n * sizeof(int));
printf("Enter values\n");
for(i=0; i<n; i++)
scanf("%d", &p[i]);
for(i=0; i<n; i++)
{
for(j= i+1; j<n; j++)
{
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Program
Program (cont.)
17
18
19
20
21
22
23
24
25
26
27
28
29
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
10
Write a C program to find square of numbers using calloc
Enter value of n: 3
Enter values
3
2
5
Square of 3 = 9
Square of 2 = 4
Square of 5 = 25
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,n;
int *p;
printf("Enter value of n: ");
scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
printf("Enter values\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
printf("Square of %d = %d\n", p[i], p[i] * p[i]);
free(p);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Program
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
11
Write a C program to add/remove item from a list using realloc
printf("Enter new size of list: ");
scanf("%d", &n2);
fp = realloc(fp, n2 * sizeof(int));
if(n2 > n1)
{
printf("Enter %d numbers\n", n2 - n1);
for(i = n1; i < n2; i++)
scanf("%d", &fp[i]);
}
printf("The numbers in the list are\n");
for(i = 0; i < n2; i++)
printf("%d\n", fp[i]);
}
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i, n1, n2;
int *fp;
printf("Enter size of list: ");
scanf("%d", &n1);
fp=(int *) malloc (n1 * sizeof(int));
printf("Enter %d numbers\n", n1);
for(i = 0; i < n1; i++)
scanf("%d", &fp[i]);
printf("The numbers in the list are\n");
for(i = 0; i < n1; i++)
printf("%d\n", fp[i]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Program
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Program (cont.)
Prof. Nilesh Gambhava
#3110003 (PPS) – Introduction to C
12