Memory management plays a crucial role in programming. There are two types of memory allocation in C language, SMA and DMA. Static Memory Allocation(SMA) is done using a variable whereas Dynamic Memory Allocation(DMA) is done using of a pointer.
What is Static Memory Allocation?
SMA is the process of allocating memory for variables during compile time. This allocation is done at the compile-time and is fixed in nature. It means that the size given to the variables cannot be changed during the execution of the program.
During compilation of the program, the compiler reserves a fixed amount of memory for declared variable based on its datatype. This memory allocation remains constant throughout the execution of the program.
Examples of Static Memory Allocation in C:
1. Static Integer Variable:
#include <stdio.h>
int main() {
static int number = 10; // Static allocation of an integer variable
printf("The value of number is: %d", num);
return 0;
}
In this example, the variable number is allocated statically with the value 10. Memory for number is allocated during compile-time, and it retains its value throughout the execution of program.
2. Static Array:
#include <stdio.h>
int main() {
static int array1[5] = {1, 2, 3, 4, 5}; // Static allocation of an integer array
for (int i = 0; i < 5; i++) {
printf("%d ", array1[i]);
}
return 0;
}
Here, an integer array array1 of size 5 is statically allocated with initial values. The memory for the array is reserved during compile-time, and the values remain constant throughout the program.
Example:
#include<stdio.h>
void main() {
int a, arrayone[7];
printf("Enter 7 values: \n");
for(a=0; a<7; a++) {
scanf("%d", &arrayone[a]);
}
printf("Printing the array: \n");
for(a=0; a<7; a++) {
printf("%d", arrayone[a]);
}
}
Output:
Enter 8 values:
10
-5
2
100
58
23
-90
51
Printing the array:
10
-5
2
100
58
23
-90
51
What is Dynamic Memory Allocation?
Dynamic Memory allocation refers to allocating memory to variables at run time. The size of the variables can be adjusted at runtime according to the need.
Dynamic Memory Allocation (DMA) is done at run time. It is flexible in nature which means that the size of the variable can be modified according to the program and the allocation of this program can be done by the following four functions.
Dynamic Memory Allocation Functions in C:
Dynamic memory allocation(DMA) is done with the help of a pointer as the four functions that are used in DMA (calloc, malloc, realloc, free) works on the concept of the pointer.
- malloc()
- calloc()
- realloc()
- free()
All the four functions written above are defined in the stdlib header file. The user has to include the stdlib header file (stdlib.h)Â while using calloc(), malloc(), realloc() and free() functions.
malloc() in C
malloc() function reserves a block of memory of a specified number of bytes. malloc stands for memory allocation. It returns a pointer of void type so that the user can typecast it into whatever the pointer they want.
If the users want to work on integer pointer , they can typecast it into integer  type pointer. If the users want to work with char pointer then they can typecast it into character type pointer.
malloc() function takes a single argument. It takes size and returns a void pointer. It does not initialize the block of memory and return a NULL pointer if the allocation fails.
malloc() function is similar to calloc() function but it does not only allocate the memory but also able to make the partition in different-different sizes.Â
For this, a structure data type is used and that is why it only takes one argument i.e; the size of the memory needs to allocate.
Syntax:
ptr = (dataType *)malloc(byte_size);
Example:
ptr1 = (int *)malloc(5*sizeof(int));
ptr2 = (char *)malloc(6*sizeof(char));
Example: malloc() function in C
#include<stdio.h>
#include<stdlib.h>
void main() {
int *ptr, n, i;
printf("Enter number of elements: \n");
scanf("%d", &n);
ptr = (int *)malloc(n*sizeof(int));
if (ptr == NULL) {
printf("Allocation Failed \n");
exit(0);
}
else {
for(i=0; i<n; i++) {
scanf("%d", ptr+i);
}
}
printf("Elements are: \n");
for(i=0; i<n; i++) {
printf("%d \n", *(ptr+i));
}
free(ptr);
}
Output:
Enter number of elements:
15
2
45
63
21
66
20
35
57
66
50
44
55
35
40
5
Elements are:
2
45
63
21
66
20
35
57
66
50
44
55
35
40
5
calloc() in C
calloc stands for contiguous allocation. It reserves a number of blocks in memory and initializes every byte with 0. It returns a NULL pointer when the allocation of memory fails.
calloc function is used for allocating multiple blocks of memory and initializes it with 0. It takes two arguments (number and byte_size) and returns a void pointer.
calloc() takes two arguments viz., number of blocks and size of each block.
Accordingly, it allocates the memory and returns the address of the same and since the address is of void types. So, the user needs to typecast it into some datatypes and this depends upon the datatypes of the pointer the user is taking to keep this address.
Syntax:
ptr = (dataType *)calloc(n, element_size); // n is no. of blocks
Example:
ptr1 = (int *)calloc(5, sizeof(int));
ptr2 = (char *)calloc(6, sizeof(char));
Example: calloc() function in C
#include<stdio.h>
#include<stdlib.h>
void main() {
int *pntr, n, i;
printf("Enter number of elements: \n");
scanf("%d", &n);
pntr = (int *)calloc(n, sizeof(int));
if (pntr == NULL) {
printf("Allocation Failed \n");
exit(0);
}
else {
for(i=0; i<n; i++) {
scanf("%d", pntr+i);
}
}
printf("Elements are: \n");
for(i=0; i<n; i++) {
printf("%d \n", *(pntr+i));
}
free(pntr);
}
Output:
Enter number of elements:
10
1
5
3
1
6
0
15
7
56
4
Elements are:
1
5
3
1
6
0
15
7
56
4
realloc() in C
realloc() function is used for changing the size of the previously allocated memory. realloc stands for re-allocation. It reallocates the size of the memory that is already being allocated in a program.
realloc function is used to resize the memory that has been already allocated in the program and that's the reason it takes only two arguments. First argument is pointer name and the second is a new size.
Syntax:
realloc(pointer_name, new_size);
Example:
// ptr1 is previously allocated pointer name.
realloc(ptr1, 10*sizeof(int));
free() in C
free() function is used for releasing the space. It frees the memory that is dynamically allocated during run time of the program.
Generally, memories that are allocated to the program at compile time will automatically deallocated when the program ends. But the dynamic memory allocation will not deallocate. As a result, every time the programme is run, memory is lost.Â
So, free() function is mandatory to use in order to deallocate this memory. It takes only one argument (pointer name which is to be deallocated).
Syntax:
free(pointer_name);
Example:
free(ptr1); //prt1 is previously defined pointer
Examples of Dynamic Memory Allocation in C:
Example 1: Allocating Memory for an Integer
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
*ptr = 10;
printf("Value stored at allocated memory: %d\n", *ptr);
free(ptr);
return 0;
}
Example 2: Allocating Memory for an Array of Integers
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 5;
arr = (int *)calloc(size, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Elements stored in allocated memory:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Example 3: Program to find the greatest number
#include<stdio.h>
#include<stdlib.h>
int main() {
int *pntr, n, i, temp;
printf("Enter number of elements: \n");
scanf("%d", &n);
pntr = (int *)calloc(n, sizeof(int));
if (pntr == NULL) {
printf("Allocation Failed \n");
exit(0);
}
else {
printf("Enter %d elements: \n", n);
for(i=0; i<n; i++) {
scanf("%d", pntr+i);
}
temp = *pntr;
for(i=0; i<n; i++) {
if(temp<*(pntr+i)) {
temp = *(pntr+i);
}
}
}
free(pntr);
printf("Greatest number is: %d", temp);
}
Output:
Enter number of elements:
8
Enter 8 elements:
14
52
30
12
786
0
15
7
Greatest number is: 786