At ALX we wrote C codes, and our code has to be memory save. We use valgrind to check for memory leaks. You don’t want to know how many hours I have spent with it 😅.

My plan with this post is to give you a taste of how it works.

#3 What Is Memory Leak?

To keep it simple, programs (apps) need memory to run. They request for the memory from the OS (Operating System), and once the memory has been allocated to them, no other program will be able to use that specific memory. Memory leaks occurs when a program doesn’t return the memory allocated to it to the OS.

Creating A Program That Has A Memory Leak

The best language to create a memory leak is the one that allows you to manage your memory. I am going to use C.

Source Code

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

/**
* main - main entry point of program.
*/
int main(void) { /* create a function main */
	char * str; /* Create a variable that points to a memory (a pointer) */

	str = malloc(6); /* Request for 6 bytes of memory and store the address in str */
	strcpy(str, "Arfs6\0"); /* Copy "arfs6" into the memory str is pointing to */
	puts(str); /* Print the content of str to stdout (screen?) */
}

I was able to write this without chatgpting or googling, just pure man page. It’s heart warming to know C is still in me 😂

The above code requests for 6 bytes of memory, stores the text arfs6 in the memory and prints the content of the memory. There are comments explaining what each line does.

Compiling And Checking For Memory Leaks

Now, let’s compile and check for memory leaks.

We need a C compiler to compile the program. I am going to use gcc:

$ gcc main.c -o arfs6

The above command compiles the source code and stores the binary in arfs6.

Next, let’s check for memory leaks using valgrind.

The command is valgrind --leak-check=full <program>. Valgrind will run <program> and monitor the memory <program> uses.

$ valgrind --leak-check=full ./arfs6
==30023== Memcheck, a memory error detector
==30023== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30023== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==30023== Command: ./arfs6
==30023== 
Arfs6
==30023== 
==30023== HEAP SUMMARY:
==30023==     in use at exit: 6 bytes in 1 blocks
==30023==   total heap usage: 2 allocs, 1 frees, 1,030 bytes allocated
==30023== 
==30023== 6 bytes in 1 blocks are definitely lost in loss record 1 of 1
==30023==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==30023==    by 0x10917E: main (in /tmp/workbentch/arfs6)
==30023== 
==30023== LEAK SUMMARY:
==30023==    definitely lost: 6 bytes in 1 blocks
==30023==    indirectly lost: 0 bytes in 0 blocks
==30023==      possibly lost: 0 bytes in 0 blocks
==30023==    still reachable: 0 bytes in 0 blocks
==30023==         suppressed: 0 bytes in 0 blocks
==30023== 
==30023== For lists of detected and suppressed errors, rerun with: -s
==30023== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
arfs6 in workbentch  9:24 PM on Tuesday 
$ 

As you can see, valgrind detected 6 losts bytes.

Fixing The Memory Leak

If you read the source code again, you’ll notice I asked for 6 bytes of memory from my OS and never gave it back. That is not nice, so let me give it back:

	puts(str); /* Print the content of str to stdout (screen?) */
	free(str); /* Release the memory str is pointing to. */
}

I added a new line of code (2nd line) at the end of the main function. It frees the 6 bytes of memory I requested.

Here is the output when I compile the code and check for memory leaks again:

$ gcc main.c -o arfs6
$ valgrind --leak-check=full ./arfs6
==30145== Memcheck, a memory error detector
==30145== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30145== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==30145== Command: ./arfs6
==30145== 
Arfs6
==30145== 
==30145== HEAP SUMMARY:
==30145==     in use at exit: 0 bytes in 0 blocks
==30145==   total heap usage: 2 allocs, 2 frees, 1,030 bytes allocated
==30145== 
==30145== All heap blocks were freed -- no leaks are possible
==30145== 
==30145== For lists of detected and suppressed errors, rerun with: -s
==30145== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
arfs6 in workbentch  9:33 PM on Tuesday 
$ 

I miss

writing an unfamiliar language. I have been focused on python, but I’ll venture into the C# world later this year.

Any suggestions? My email is below :)