Updated March 28, 2023
Introduction to Memory Allocation in C
Memory allocations, in general, mean where computer programs and services are executed to reserve partially or complete space or virtual memory of a computer, this process is known as memory allocation. This process is hardware operation and is achieved by memory management through Operating systems and software applications. In general, there are static and dynamic memory allocations, whereas, in C programming language, we will see about dynamic memory allocation where programs are allocated during run time in memory and static memory allocation is a process of allocating memory while writing the C program which means memory is allocated at compile time.
How does Memory Allocation work in C?
In C language, static and dynamic memory allocation is also known as stack memory and heap memory which are allocated during compile time and run time, respectively.
1. Static Memory Allocation
As we discussed static memory allocation is the allocation of memory for the data variables when the computer programs start. This type of allocation is applied to only global variables, file scope variables and also to those variables that are declared as static. This type of allocation is having a drawback when you are allocating memory we should know the exact memory before allocating as this process allocates fixed memory and cannot be changed after allocating.
1. There are a few features of static memory allocation. They are: this type of allocation allocates variables permanently; hence the memory in this type of allocation cannot be reused and is, therefore, less efficient. This allocation uses the stack for implementing the allocation process.
Let us see an example below:
Code:
void play
{
int x;
}
int main()
{
int y;
int c[10];
return 1;
}
Explanation: In the above program, the variables x, y, and care statically allocated so the memory is strictly allocated at compile time for the variable data. Note that the deletion of memory is necessary when the variables are not in use because it will lead to memory leakage. Therefore in static memory allocation, it automatically frees the memory based on the scope of the variable which means as soon as the variable’s cope is over the memory gets released.
2. A variable can internally or externally be declared as static in which its value persists until the end of the program, where this can be done using the keyword static before the variable declaration. There can be internal or external static variables that are declared inside or outside the function.
Let us see an example:
#include<stdio.h>
void stat(void);
int main()
{
int i;
for(i=1; i<=3 ; i++)
stat();
return 1;
}
void stat(void)
{
static int n = 0;
n = n+1;
printf("n = %d""\n", n);
}
Output:
2. Dynamic Memory Allocation
As discussed above dynamic memory allocation is allocation of memory during runtime or during program execution. Dynamic memory allocation provides different functions in the C programming language. They are: malloc(), calloc(), realloc(), free(). Let us see in detail.
1. malloc()
This method allocates the space in the memory during execution but will not initialize the memory allocation during execution as it carries garbage values and if it cannot allocate requested memory then it returns a null pointer.
Syntax:
(CastType*) malloc(size);
Code:
mptr = (int*) malloc(100 * sizeof (int));
In the above example, the statement allocates 200 bytes of memory because the int size in C is 2 bytes and the variable mptr pointer holds the address of the first byte in the memory.
2. calloc()
This is also known as contiguous allocation. As in malloc() will not initialize any memory bit. But in calloc() it allocates the memory along with initializing the bits to zero.
Syntax:
(CastType*) calloc(n, size)
Code:
cptr = (int*) calloc(35, sizeof (int));
In this function, the above example statement allocates contiguous memory space for about 35 elements of data type “int”.
3. free()
As discussed above the memory space should be freed or released if not in use. In Dynamic memory allocation, malloc() and calloc() function only allocates memory but cannot free the memory on their own so this is done using free() method explicitly to release the memory that is not in use to avoid memory leakage.
Syntax:
free (pointer_variable);
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p, *p1;
int x, i;
x = 5;
printf("Enter number of elements to allocate in memory : %d\n", x);
p = (int*)malloc(x * sizeof(int));
p1 = (int*)calloc(x, sizeof(int));
if (p == NULL || p1 == NULL) {
printf("Memory is not allocated.\n");
exit(0);
}
else {
printf("Memory has been successfully allocated using malloc.\n");
free(p);
printf("Malloc Memory has been successfully freed or released.\n");
printf("\nMemory has been successfully allocated using calloc.\n");
free(p1);
printf("Calloc Memory has been successfully freed or released.\n");
}
return 0;
}
Output:
4. realloc()
As the name suggests, in dynamic memory allocation if suppose a user wants to allocate more memory which means more memory than specified or required by the program then we can use this realloc() function to alter the size of memory that was allocated previously.
Syntax:
realloc (pointer_variable, n);
Code:
Suppose if we want to change the size of memory from 200 bytes to 600 bytes. Let us how it can be done using realloc().
char *rptr;
rptr = malloc(200);
rptr = realloc(rptr, 600);
Conclusion
Memory allocation in C programming language is simple using static memory allocation which allocates memory during compile time or we can say before the program execution and it also has another type known as dynamic memory allocation which allocates memory during run time or allocating memory during program execution which uses 4 different functions such as malloc(), calloc(), free() and realloc(). There are different pros and cons of both methods.
Recommended Articles
This is a guide to Memory Allocation in C. Here we discuss static and dynamic memory allocation in C language with functions. You can also go through our other related articles to learn more –