In the C programming language, the following four functions are used whenever we talk about dynamic memory management: malloc(), calloc(), realloc(), and free(). However, the scope of today’s article is limited to the malloc() function in C, which stands for “memory allocation.” We will take a look at its usage and try to justify why we need this function in the first place. Finally, we will conclude our article after walking you through an example of using the malloc() function in the C programming language.
Usage and Need of using “malloc” in C
Whenever we talk about dynamic memory allocation, we literally mean that the memory will be allocated at run time instead of compilation time. It means that even if the memory increases or decreases during program execution, it can be handled very gracefully. This accounts for efficient memory management in C. It lets you reserve the desired memory and conserves your memory when it is not in use.
The malloc() function is such a function in the C programming language that assigns a single block of the requested memory. It is a part of the “stdlib.h” library in C. Its general declaration is as follows:
As you can see from the function declaration, it allocates the specified size of uninitialized memory. The memory is initialized at run time, which means that the pointer only points to the first block of the specified memory before execution. In other words, we can say that no extra space is reserved until your program is executed. The “cast type” used in the declaration is there to specify the type of data to which the pointer points.
In this way, using the malloc() function in C can do dynamic memory allocation very conveniently. We will go through an example that will demonstrate how this function can be used in the C programming language. Another important thing that we want to highlight over here is that the malloc() function (in fact, the realloc() and calloc() functions as well) is used in conjunction with the free() function. It is so because by using these functions, we are handling memory allocation manually. Therefore, as soon as the execution finishes, we will have to free up the memory manually. Otherwise, it will remain reserved, and you may run out of space in the future.
Example of using “malloc” in C
For using the malloc() function in C, we have devised a very basic example in which we wanted to print the values of an array that are allocated dynamically. For that, we have created a C file named malloc.c. In this file, we have a C program in which the two required libraries are included first. After that, we have created our “main()” function. This function starts with the creation of an integer type pointer. We have only declared this pointer at this step.
Then, we wanted to assign a value to this pointer. We wanted this pointer to point to the first index of an integer array whose values will be allocated dynamically. That is why our cast type is “int*.” We wanted this array to hold 5 integer values. That is why we have multiplied our byte size by 5. You can have any value over here or even take this value as input from the user.
After doing this dynamic memory allocation, we have an “if” statement to check if the value of our pointer is NULL so that it can print an error message and exit. However, if the value of our pointer is not NULL, then our code will move to the “else” statement in which we have printed a success message first.
Then, we have a “for” loop in which we assign values to our dynamically allocated array. In the next “for” loop, we print those values that we have just assigned to our array. Finally, outside the “else” part of our code, we free up our dynamically allocated memory with the free() function.
After saving this code, we have compiled it with the following command:
Once the code is compiled successfully, we executed its object file with the command shown below:
In the output of our C program that we have created in this example, you will be able to notice that our dynamic memory allocation has taken place successfully. Moreover, the values assigned to the different indexes of our array as a result of the dynamic memory allocation are also shown in the following output image:
Conclusion
This article educated you on the importance of dynamic memory management in the C programming language. Automatic memory management indeed saves you from all the hassle of managing things manually. However, at times, it is not feasible to do so as your memory requirement might increase or decrease during the execution of your C program. In these situations, you can easily use the “malloc()” function in C as we did in today’s example.
from Linux Hint https://ift.tt/3oTH6fi
0 Comments