What Is Memory Leak in C

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What do you mean by memory leak?

A condition caused by a program that does not free up the extra memory it allocates. … When those memory areas are no longer needed, the programmer must remember to deallocate them. When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer).

How do you prevent memory leaks in C?

  1. Every malloc or calloc should have a free function:
  2. Avoid the orphaning memory location.
  3. Create a counter to monitor allocated memory.
  4. Do not work on the original pointer.
  5. Write the proper comments.
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest