Memory Allocation in C
Memory allocation is an essential part of programming in C. It is a process of explicitly reserving memory for a particular code snippet. Memory management is at the very core of writing good code in C. Here comes the memory allocation, which allows us to manage the available memory efficiently.
Types of Memory Allocation
There are various ways of allocating and deallocating memory in C. C programming language supports three types of memory allocation.
1. Static memory allocation
2. Dynamic memory allocation
3. Automatic memory allocation
Static Memory Allocation
Static memory allocation in C is a type of memory allocation in which memory is allocated at compile time, and the memory is allocated before the program starts running. In this type, the memory remains allocated till the program executes. In C, memory allocation is done using the static keyword.
Code:
#include <stdio.h> int main() { static int num = 10; // static variable declaration and initialization printf("The value of num is %d\n", num); num++; // increment the value of num printf("The value of num after increment is %d\n", num); return 0; }
The variable named num is declared using the static keyword, which tells the compiler to allocate memory for num at compile time.
Output:
This shows how the num variable is still allocated after incrementing the value of it.
Automatic Memory Allocation
In automatic memory allocation, memory is allocated by the compiler automatically. In this type of memory allocation, the compiler allocates memory to blocks of code from a stack memory space. Stack is where local variables and function calls are stored. In this case, the compiler allocates and frees memory, so the programmer need not worry about that.
Code:
#include <stdio.h> int main() { int num = 10; // automatic variable declaration and initialization printf("The value of num is %d\n", num); num++; // increment the value of num printf("The value of num after increment is %d\n", num); return 0; }
The program declares the num variable, which enters the stack memory when the main function is called. Allocated memory is released when the main function gets executed.
Output:
This shows how automatic memory allocation works in C. num variable was allocated when the main function is called, and the memory is automatically deallocated when the function returns.
Dynamic Memory Allocation
Dynamic memory allocation is just the opposite of static memory allocation. In dynamic memory allocation, the memory is allocated at run time, whereas in static memory allocation, memory is allocated at compile time. This type of memory allocation is allocated from a particular region of memory called the heap. The heap is the pool of memory locations that are available for the program to use.
In C, dynamic memory allocation is achieved using functions like malloc, calloc, and realloc.
Example of doing dynamic memory allocation using malloc function.
Code:
#include <stdio.h> int main() { int num = 10; // automatic variable declaration and initialization printf("The value of num is %d\n", num); num++; // increment the value of num printf("The value of num after increment is %d\n", num); return 0; }
Here we are declaring a pointer to the variable numPtr. Using the malloc function, we are allocating memory for an integer. Then we assign the address of the allocated memory to numPtr. We use the * operator to dereference the pointer and then assign a value of 10 to the variable. Here the free function is used to deallocate the memory.
Output:
Now we will see the use of another function calloc, to achieve dynamic memory allocation.
Code:
#include <stdio.h> #include <stdlib.h> int main() { int *numPtr; // pointer to an integer numPtr = (int*) calloc(1, sizeof(int)); // allocate memory for an integer and initialize it to 0 printf("The value of num is %d\n", *numPtr); *numPtr = 10; // assign a value to the integer printf("The value of num after assignment is %d\n", *numPtr); free(numPtr); // release the memory return 0; }
In this code, we are declaring a pointer to a variable numPtr. Then using the calloc function, we allocate memory to an integer and initialize it by 0. The first argument in calloc is the number of elements, and the second argument is the size of the elements. We print the current value of the integer using printf. Then we assign a value of 10 to the integer and dereference the pointer using the * operator. Here is the output of the above code.
Output:
Now we will use another function called realloc to do dynamic memory allocation.
Code:
#include <stdio.h> #include <stdlib.h> int main() { int *numPtr; // pointer to an integer numPtr = (int*) malloc(sizeof(int)); // allocate memory for an integer *numPtr = 10; // assign a value to the integer printf("The value of num is %d\n", *numPtr); numPtr = (int*) realloc(numPtr, sizeof(int) * 2); // resize the memory to hold two integers *(numPtr + 1) = 20; // assign a value to the second integer printf("The value of num after resizing is %d\n", *numPtr); printf("The value of the second integer is %d\n", *(numPtr + 1)); free(numPtr); // release the memory return 0; }
In the above example, we do the usual dynamic memory allocation process using the calloc function. After then, we use the realloc function to reallocate the memory to hold two integers. The first argument is the pointer to the variable, and the second argument is the new size of the memory block. Then we assign a value of 20 to the variable and print the value using the printf function. At last, we use the free function to deallocate the memory allocated to the variable. Here is the output of the code above.
Output:
This shows how dynamic memory allocation is done by using the relloc function.
We have now understood the various types of memory allocation in the C programming language.