Skip to content

Latest commit

 

History

History
107 lines (78 loc) · 3.16 KB

3-valgrind.md

File metadata and controls

107 lines (78 loc) · 3.16 KB

Checking for Memory Issues with Valgrind

This section introduces Valgrind, a powerful tool for detecting memory issues in your C programs. Understanding how to use Valgrind is essential for writing safe, bug-free C code.


1. What is Valgrind?

Valgrind is a tool that helps you find:

  • Memory leaks (memory you forgot to free)
  • Invalid memory access (such as accessing memory after it was freed)
  • Use of uninitialized memory
  • Heap usage statistics

Valgrind runs your program in a special environment and checks every memory operation.


2. How to Use Valgrind

First, compile your program as usual:

gcc foo.c -o foo

Tip: For best results, add the -g flag:

gcc -g foo.c -o foo

This includes debugging information (see below for why this helps).

Then, run your program with Valgrind:

valgrind ./foo

or

valgrind ./foo | less

3. Understanding Valgrind Output

When you run Valgrind, you'll see several important lines. Here’s what they mean:

==2574==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==2574==
==2574== All heap blocks were freed -- no leaks are possible
==2574==
==2574== For lists of detected and suppressed errors, rerun with: -s
==2574== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  • total heap usage: Shows how many times your code called memory allocation functions (like malloc/free) and the total amount of memory allocated.
  • All heap blocks were freed -- no leaks are possible: Good news! Your program did not leak any memory.
  • For lists of detected and suppressed errors, rerun with: -s: Tells you how to get more detailed error lists.
  • ERROR SUMMARY: Number of memory errors Valgrind found. "0 errors" means your program handled memory correctly.

4. The Importance of the -g Flag

When you compile with -g, you include debugging information in your program.
This makes Valgrind's output much more helpful.

Example: With and Without -g

With -g:

==12345== Invalid write of size 4
==12345==    at 0x4005A6: main (foo.c:8)

Valgrind points to the exact file and line number (foo.c:8).

Without -g:

==12345== Invalid write of size 4
==12345==    at 0x4005A6: main+0x16 (0x4005a6)

Valgrind only shows a cryptic address. You won't know where the problem is in your source code.

Takeaway:
Always compile with -g during development! It makes finding and fixing memory bugs much easier.


5. Extra Tips

  • Use valgrind --leak-check=full ./foo for even more detailed reports.
  • Compile with both -g (for debugging info) and -O0 (to disable optimizations) for the most accurate results:
    gcc -g -O0 foo.c -o foo
  • Valgrind works best for programs that use dynamic memory (malloc, calloc, free, etc.).

6. Summary

  • Valgrind helps you catch memory bugs before they become serious problems.
  • Key output lines show heap usage, leaks, and error summaries.
  • Compiling with -g gives you source code line numbers, making debugging much easier.
  • Always check Valgrind's output after changes to your code, especially if you use dynamic memory.