Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 48 of 50 · c

Memory Debugging with Valgrind

Duration: 10 mins

Valgrind detects memory leaks, invalid reads/writes, and use‑after‑free.

Typical usage:

valgrind --leak-check=full ./myprog arg1 arg2

Output includes:

  • Invalid read/write – address out of bounds.
  • Leak summary – bytes lost, reachable, still reachable.
  • Suppression files – ignore known false positives.

📚 Example: Leak Detection

int main(void) {
    int *p = malloc(10 * sizeof *p);
    p[0] = 42; // good
    // forgot free(p);
    return 0;
}

Running Valgrind will report a definitely lost block of 40 bytes.

🎯 Best Practices

  • Compile with -g to get meaningful line numbers.
  • Run under Valgrind regularly during development.
  • Combine with --track-origins=yes to pinpoint uninitialised values.
Previous: Static Analysis with clang‑tidyNext: Cross‑Compilation – Building for Embedded Targets