How do you use Valgrind and AddressSanitizer to find memory bugs?
AddressSanitizer and Valgrind find leaks, overflows and use after free in C++ programs. See the commands, sample reports and when to use each tool.
337 posts about C++: questions with answers, tutorials and articles, newest first.
AddressSanitizer and Valgrind find leaks, overflows and use after free in C++ programs. See the commands, sample reports and when to use each tool.
A buffer overflow in C++ is an out-of-bounds read or write that corrupts memory. See what causes it and how safe types, bounds checks and tools prevent it.
A dangling pointer in C++ refers to an object that no longer exists. Learn the typical causes, safe ownership patterns and how to catch the bug with tools.
A memory leak in C++ is heap memory that can no longer be freed. See the usual causes, how to detect leaks with sanitizers, and how RAII prevents them.
new[] allocates a heap array and delete[] destroys and frees it. See initialisation, internals, common bugs and modern replacements like std::vector.
The One Definition Rule limits how often C++ entities may be defined. Learn the linker errors, the silent violations and how inline and templates stay legal.
The C++ preprocessor rewrites your source before compilation. Learn how macros work, why they bite, and which modern features replace them.
Split a C++ program by putting declarations in headers and definitions in .cpp files. See a worked calculator example, build commands and common link errors.
Header files share declarations between C++ source files, and include guards stop them being pasted twice. Learn #ifndef, #pragma once and common mistakes.
Storage classes in C++ decide how long a variable lives and which files can see it. See static, extern, thread_local and mutable with examples.
Understand where C++ names are visible and how long objects live, covering block scope, globals, static locals, shadowing and dangling pointers.
Learn how recursion works in C++ with factorial, digit sum and Fibonacci examples, plus stack limits, memoisation and recursion versus loops.
Learn what const correctness means and how pointer to const differs from const pointer, with a reading rule, a table and runnable examples.
Learn how adding and subtracting pointers moves by whole elements, how it relates to array indexing, and the bounds rules that keep it safe.
Pointers can be null and rebound, references cannot. See the key differences with examples and learn which to choose in your own code.
A beginner guide to pointers in C++: declaring, dereferencing, null pointers, arrays and dynamic memory, with small complete examples.
Default arguments let callers omit trailing parameters in C++. See the syntax, rules, pitfalls with overloads and a modern options struct alternative.
See how C++ passes arguments by value, reference and pointer, with runnable examples and simple rules for choosing the right parameter type.
Define functions with a return type, name and parameters, declare them before use, and call them with arguments, with overloading and modern features.
Undefined behavior lets a C++ program do anything. See the common causes, why optimisers make it worse, and the tools and habits that prevent it.