Fundamentals 6 min read

Understanding C Memory Leaks: Causes, Detection, and Prevention

This article explains why memory leaks occur only with heap allocations in C, describes how malloc and related functions allocate memory, outlines two common ways to obtain heap memory, identifies the three essential elements of a leak, debunks common freeing misconceptions, and provides practical inspection steps to prevent leaks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Understanding C Memory Leaks: Causes, Detection, and Prevention

1. Memory Leak Principles

1.1 How heap memory is stored in C

Memory leaks only appear when using heap memory; stack memory is automatically allocated and released. In C, heap memory is obtained with malloc. A typical allocation looks like:

char *info = NULL;
info = (char*)malloc(NB_MEM_SPD_INFO_MAX_SIZE);
if (NULL == info) {
    (void)tdm_error("malloc error!
");
    return NB_SA_ERR_HPI_OUT_OF_MEMORY;
}

Because malloc returns an address, the variable that holds the heap memory must be a pointer, which may be a single‑level or multi‑level pointer.

1.2 Methods to obtain heap memory

There are two common patterns for acquiring heap memory:

Method 1 – Assign the function’s return value to a pointer

char *local_pointer_xx = NULL;
local_pointer_xx = (char*)function_xx(para_xx, ...);

Functions that return a pointer (e.g., g_slist_append) follow this pattern.

Method 2 – Pass a pointer‑to‑pointer as an output parameter

int ret;
char *local_pointer_xx = NULL;
ret = (char*)function_xx(..., &local_pointer_xx, ...);

Typical APIs such as getline(char **__lineptr, size_t *__n, FILE *__stream) use a double pointer to receive the allocated memory.

Both methods ultimately allocate memory inside the called function; the difference lies in whether the pointer is returned directly or via an output argument.

1.3 Three elements of a memory leak

1) The function defines a local pointer variable.

2) The pointer receives heap memory through one of the two acquisition methods.

3) Before the function returns—whether through normal execution or an error path—the memory is neither freed nor transferred to a global variable or returned to the caller.

1.4 Common misconceptions about freeing memory

Many developers assume only memory allocated directly with functions like malloc or g_malloc needs to be freed, or only memory obtained via familiar APIs requires release. This narrow view causes missed frees when using unfamiliar interfaces that allocate memory internally, leading to leaks.

2. Inspecting Memory Leaks

Adopt a disciplined review process aligned with the three leak elements:

When a local pointer appears, treat it as a potential leak source and investigate further.

Check whether the pointer was assigned via one of the two heap‑acquisition methods; if so, determine what the pointer ultimately references.

Identify whether the memory is intended to become global, static, or be returned. If none apply, ensure every allocation is freed on all return statements, including error branches.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingCmemory leakHeapmallocprogramming fundamentals
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.