In this section, we will learn what the malloc() function is and how to use it in C.
Dynamic Memory Allocation in C: malloc() Function
When we create a variable of any type, its memory space is automatically calculated and set aside, and there’s no need for a programmer to calculate this space.
The pros of this action is that the programmers are relieved from the pressure of this calculation but on the other hand, the cons would be that the memory space is static for the variable and we can’t dynamically increase the space at runtime (when the program is running).
For example, let’s say we have a program that has an array which can store 100 elements. For the majority of times, this is what our program needs! But there are times that we need to store 200 elements! In those situations, the program is useless! One solution might be to redesign the program in a way to be able to store 200 elements in the first place. But this design will waste the memory space because we don’t need that much of memory space for the majority of times when the program is running.
This is where we can use the dynamic memory space that can increase its size when it’s needed.
One of the functions that allows us to allocate dynamic memory space is the `malloc` function.
The prototype of this function exists in the `stdlib.h` header file and so we need to include the header file in order to use the function.
C malloc() Function Syntax:
Here’s the prototype of the function:
void *malloc(size_t size)
C malloc() Function Parameters:
This function takes one argument and that’s the number of bytes of memory that we want to be set aside for a variable.
C malloc() Function Return Value:
The returned value of this function is the address to the starting point of the memory space that is set aside by this function.
Note: the returned pointer of the function is a general pointer and so we need to cast it to the related type. For example, if we’re going to store integer variable in this memory space, we then need to cast the returned pointer to a pointer of type int like `(int *)`.
Example: using malloc() function in C
#include <stdio.h> #include <stdlib.h> int *pointer; int main() { int size; printf("Please enter the number of elements: \n"); scanf("%d",&size); pointer = (int *) malloc(size * sizeof(int)); for (int i = 0 ; i< size; i++){ *(pointer+i) = i; } printf("The values stored in the array are: \n"); for (int i = 0; i<size; i++){ printf("%d, ",*(pointer + i)); } free(pointer); return 0; }
Output:
Please enter the number of elements: 10 The values stored in the array are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
How to use malloc() function?
In this example, we’ve asked the user to enter how many elements she wants to store in the program.
The input value is 10 but this is not the actual number of bytes that should be set aside in the memory! Because each integer value takes 4 bytes in one system and 8 bytes in another, we used the `sizeof` operator in order to get the actual size of one integer value and then multiplied that value to the number 10. The result of this expression is the actual size of the memory space that we need in order to store 10 integer values.
After that, the `malloc` function returned a general pointer to the first byte of this dynamically allocated memory space and we cast it and assigned the address to the variable `pointer`.
Note:
When we create a variable of type integer or double or float etc. the compiler takes care of creating and cleaning the memory space allocated to the variable automatically.
But when we access a memory space via the call to a function like `malloc` or `calloc`, that memory space won’t go anywhere until the end of the program and so it’s on us as developers to take care of cleaning this memory space when we’re done with it.
This is where the function named `free` comes in. You’ll learn more about this in the `free function` section, but in short, this function takes one argument and that is the address returned from the `malloc` or `calloc` function.
The golden rule is that for each call to `malloc` or `calloc` function, there should be another call to the `free` function at some point in the program to make sure the memory-space is released when it’s no-longer needed.
So here in our example, the last line of execution in the body of the `main` function is to call the `free` function in order to release the memory space allocated via the call to the `malloc` function.