Go back to blog listing

Finding Memory Leaks in C or C++

A literal leak

Unlike the picture above, memory leaks in software can be hard to pinpoint. Learn how to find memory leaks in C and C++ applications with the help of a runtime error detection tool. 

What is a memory leak?

Those writing code in C or C++ will be familiar with memory leaks. Wikipedia offers the following definition:

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.

In other words, leaks mean that dynamically-allocated memory cannot be released back to the operating system because the program no longer contains pointers that can access it. You've lost control of that piece of memory and can no longer access it or free it. 

A simple example of this behavior can be seen by running the “Hello world” program showing below.

* File: hello.c
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
char *string, *string_so_far;
int i, length;     length = 0;
for(i=0; i<argc; i++) {
length += strlen(argv[i])+1;
string = malloc(length+1);

/* * Copy the string built so far. */
if(string_so_far != (char *)0)
strcpy(string, string_so_far);
else *string = '\0';
strcat(string, argv[i]);
if(i < argc-1) strcat(string, " ");
string_so_far = string;
printf("You entered: %s\n", string_so_far);
return (0);

If we execute this program with the following arguments:

   hello this is a test

If we examine the state of the program at line 25, just before executing the call to malloc for the second time, we observe:

  • The variable string_so_far points to the string “hello” which it was assigned as a result of the previous loop iteration.
  • The variable string points to the extended string “hello this” which was assigned on this loop iteration.

These assignments are shown schematically below; both variables point to blocks of dynamically allocated memory.

The next statement:

string_so_far = string;

will make both variables point to the longer memory block as shown below:

Both variables point to longer memory block

Once this happens, however, there is no remaining pointer that points to the shorter block. Even if you wanted to, there is no way that the memory that was previously pointed to by string_so_far can be reclaimed; it is now permanently allocated. This is known as a “memory leak.”

How do you find and fix memory leaks?

This type of error can be diagnosed by memory error detection tools, such as Parasoft Insure++. This is shown below:

[hello.c:25] **LEAK_ASSIGN**
>>         string_so_far = string;
  Memory leaked due to pointer reassignment: string
  Lost block : 0x0804bd68 thru 0x0804bd6f (8 bytes)
               string, allocated at hello.c, 15
                          malloc()  (interface)
                            main()  hello.c, 15
  Stack trace where the error occurred:
                            main()  hello.c, 25

This example is called LEAK_ASSIGN because it is caused when a pointer is re-assigned. (P.S. Other memory debuggers often don't make a distinction between outstanding memory and actual leaked memory, but Insure++ does.) In this case, outstanding memory isn't memory that's awesome, it's memory that you didn't free, as distinct from an actual leak which is memory that you cannot free.

Types of memory leaks

Parasoft Insure++ can also detect several other types of leaks automatically:

Leak type



Occurs when you free a block of memory that contains pointers to other memory blocks.


Occurs when a function returns a pointer to an allocated block of memory, but the returned value is ignored in the calling routine.


Occurs when a function contains a local variable that points to a block of memory, but the function returns without saving the pointer in a global variable or passing it back to its caller.

Notice that the error message indicates the exact source line on which the problem occurs, not just where the block was allocated, which is a key issue in finding and fixing memory leaks. This is extremely important because it’s easy to introduce subtle memory leaks into your applications, but very hard to find them all.

To start cleaning memory leaks from your code, you can get a free trial of Parasoft Insure++.

Get the ultimate memory debugger for C and C++

Stay up to date