Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Memory Leak in C

Last Updated on March 29, 2023 by Prepbytes

Memory Allocation is an important concept of C programming. It is quite beneficial in several situations but might also lead to Memory Leaks in C when a program allocates memory dynamically but the program fails to free it properly. This problem must be avoided. In this article, we will discuss Memory Leak in C and ways how to avoid Memory Leak in C. So, let us deep dive into the topic.

What is Memory Leak in C?

Memory Leak in C is defined as the specific type of resource leakage which occurs when a software or program allocates memory using dynamic memory allocation functions such as malloc(), calloc(), and realloc() but fails to release it using the free() function. This results in a situation where allocated memory remains in use even after it is no longer needed.

Since there is only a limited amount of resources available for each system, a memory leak in C can cause the program to consume more memory. This may cause the system to malfunction or the system’s performance degrades.

Causes of Memory Leak in C

Memory Leak in C can be caused in various ways. Some of the probable causes are listed below.

  • One common cause of Memory Leak in C is due to Overwriting Pointer Variables. This means assigning a pointer variable with a different address value before de-allocating the already pointed memory block.
  • Memory Leak in C can also be caused if a pointer variable goes out of the scope.
  • Error during the deallocation process also results in the Memory Leak in C.
  • If we do not deallocate the memory before the program execution finishes, this also causes Memory Leak in C.

Examples of Memory Leak in C

Here are examples showing the Memory Leak in C.

Example 1 of Memory Leak in C
The following example demonstrates how memory leak in C programming due to overwriting of pointer variables.


#include <stdio.h>
#include <stdlib.h>

int main() {
    int ptr = (int)malloc(sizeof(int));
    ptr = (int*)malloc(sizeof(int));

return 0;

In the above example, a pointer variable ‘ptr’ is declared and assigned the address of a dynamically allocated integer using the malloc() function. However, the program allocates memory for ‘ptr’ again without first freeing the previously allocated memory, resulting in a memory leak.

Example 2 of Memory Leak in C
This example demonstrates the Memory Leak in C caused when the pointer variables become out of scope.



int main()
    int a = 12, b = 24; 
        int *sum = (int*)malloc(sizeof(int));
        *sum = a + b;
    printf("%d\n", *sum);
    return 0;


error: ‘sum’ undeclared (first use in this function)
     printf("%d\n", *sum);

In the above program C program, we have allocated a memory block to the sum pointer variables in the inner block of the code inside of the main() function. We allocated an integer memory block to the sum pointer variable in a local scope (inner block of code) inside the main() function in the prior C program. We used the sum pointer to assign the addition of a and b to the allocated memory block, but when the block scope ends, the sum pointer is destroyed and the memory block remains allocated. This will result in a memory leak in C.

How to Avoid Memory Leak in C

Here are some of the ways to avoid Memory Leak in C.

  • free() Function with every malloc() and calloc() Function:
    After each malloc (calloc) function, include a free function. Assume we need to create an array of characters to store some data in an application. Since we already know that the memory management functions (such as (malloc or calloc) will be used to construct a dynamic array in C. It is recommended to write the free function right after the malloc or calloc. It prevents the programmer from missing writing the free function. Here is a code snippet for the above scenario.

    void prep_func(int n)
      char *array = malloc (n *sizeof(char));
      // code to execute
  • Avoid the usage of Original Pointer:
    Working with a temporary pointer is always recommended because the original pointer contains the address of the allocated memory(which might get accidentally changed). So in this case, if we use a temporary pointer, it helps us in preserving the value of the original pointer. This helps in avoiding the Memory Leak in C as it restricts the problem overwriting of Pointer Variables.

    int *ptr = (int*)malloc(sizeof(int));
    // temporary pointer variable
    int *temp = ptr;

Memory leak in C is a common issue in programming. It occurs when a program allocates memory dynamically but fails to free it properly, resulting in a loss of memory resources. Memory leak in C can occur in several ways, such as forgetting to free memory and overwriting pointer variables. To prevent and detect memory leak in C programming, programmers should always free memory, use static memory allocation when possible and other methods as discussed in the article. By understanding the causes and prevention of memory leak, programmers can write more efficient and reliable programs in C.

Frequently Asked Questions(FAQs)

Here are some Frequently Asked Questions related to “Memory Leak in C”.

Ques 1. Why is a memory leak dangerous?
Ans. Memory Leak in C is dangerous because it can cause the program to consume much amount of memory over time which may result in instability of the system or may lead to a system crash.

Ques 2. Can a memory leak occur in stack memory?
Ans. No, a memory leak cannot occur in stack memory because stack memory is automatically deallocated when a function returns.

Ques 3. Can a memory leak occur in global variables?
Ans. Yes, a memory leak can occur in global variables if they are dynamically allocated and not properly deallocated.

Ques 4. What happens if you try to free memory that has already been freed?
Ans. If you try to free memory that has already been freed, it can cause undefined behavior or a program crash. This is called a double-free error.

Ques 5. Can a memory leak occur in a loop?
Ans. Yes, a memory leak can occur in a loop if memory is dynamically allocated inside the loop but not properly deallocated before the loop ends.

Leave a Reply

Your email address will not be published. Required fields are marked *