Stage 1 · Lesson 8 of 8
Undefined behaviour and debugging
(UB) is code that the C++ standard gives no meaning to, so anything can happen. One example is reading past the end of a vector with []. Others are using an uninitialised variable (one you never gave a value), overflowing a signed integer, and using a reference to something already destroyed.
The program may seem to work, crash, or give different results with different compiler settings. That is why these bugs are so hard to find. Your defences are warnings, .at() while learning, and , extra checks the compiler can build in.
-fsanitize=address,undefined with or clang++ stops the program at the bad line and explains what happened. For wrong output, use a such as gdb, or your IDE's own. Set a breakpoint, step through and watch the variables instead of guessing.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with
- CourseFreeLearnCpp.com
LearnCpp.com · Beginner
Free, current and careful: chapters 0 to 16 take you from setup to references and std::vector