what happens when you don’t free memory after using malloc()

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.

Syntax:

ptr = (cast-type*) malloc(byte-size)

For Example:

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():

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():

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate free()
// and malloc() function
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
int main()
{
  
    // Pointer to hold base address
    // of memory block
    int* p;
    int n, i;
  
    // Number of element
    n = 3;
  
    // Dynamically allocate memory
    // using malloc()
    p = (int*)malloc(n * sizeof(int));
  
    // Check if memory allocation is
    // successfull or not.
    if (p == NULL) {
        printf("Memory allocation"
               " failed.\n");
        exit(0);
    }
  
    else {
  
        // Memory allocation successful
        printf("Memory allocation "
               "successful using"
               " malloc.\n");
  
        // Insert element in array
        for (i = 0; i < n; i++) {
            p[i] = i + 2;
        }
  
        // Print the array element
        printf("The array elements"
               " are:\n");
  
        for (i = 0; i < n; i++) {
            printf("%d ", p[i]);
        }
  
        // De-allocate the allocated
        // memory using free()
        free(p);
    }
  
    return 0;
}
chevron_right

Output:
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.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :
C
Practice Tags :