Programming foundations

Stage 1 · Lesson 8 of 8

Undefined behaviour and debugging

Your first language

(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

  • CourseFree
    LearnCpp.com

    LearnCpp.com · Beginner

    Free, current and careful: chapters 0 to 16 take you from setup to references and std::vector

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • The standard library reference: check std::vector and std::string pages as you use them

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?