Pre-requisite: Dynamic memory allocation in C
The “malloc” or “memory allocation” method is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with a default garbage value.
ptr = (cast-type*) malloc(byte-size)
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.
But the memory allocation using malloc() is not de-allocated on its own. So, “free()” method is used to de-allocate the memory. But the free() method is not compulsory to use. If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally). Still, there are some important reasons to free() after using malloc():
- Use of free after malloc is a good practice of programming.
- There are some programs like a digital clock, a listener that runs in the background for a long time and there are also such programs which allocate memory periodically. In these cases, even small chunks of storage add up and create a problem. Thus our usable space decreases. This is also called “memory leak”. It may also happen that our system goes out of memory if the de-allocation of memory does not take place at the right time.
So, the use of the free() functions depends on the type of the program but it is recommended to avoid the unwanted memory issues like a memory leak.
Below is the program to illustrate the use of free() and malloc():
Memory allocation successful using malloc. The array elements are: 2 3 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- What happens if we mix new and free in C++?
- How to deallocate memory without using free() in C?
- How does free() know the size of memory to be deallocated?
- Difference Between malloc() and calloc() with Examples
- Function Interposition in C with an example of user defined malloc()
- malloc() vs new
- How will you show memory representation of C variables?
- What happens when a function is called before its declaration in C?
- What happens when more restrictive access is given to a derived class method in C++?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- How to add space (" ") after an element using :after selector in CSS ?
- How will you print numbers from 1 to 100 without using loop?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- delete and free() in C++
- How can competitive programming help you get a job?
- How do you get the length of a string in JQuery?
- fork() and memory shared b/w processes created using it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.