>
>
>
C++ programmer's guide to undefined beh…

Andrey Karpov
Articles: 676

C++ programmer's guide to undefined behavior

Your attention is invited to the complete list of e-book parts (12 parts out of 11) on undefined behavior. This is not a textbook, as it's intended for those who are already familiar with C++ programming. It's a kind of C++ programmer's guide to undefined behavior and to its most secret and exotic corners. The book was written by Dmitry Sviridkin and edited by Andrey Karpov.

For the past six months, we've been working diligently to prepare and publish a series of texts on undefined behavior in the PVS-Studio blog. It's been a great undertaking, and we're thrilled that the project is finally nearing its conclusion—or almost. This book is set to be adapted into a print edition in the future, but that's a story for another time.

Originally, we planned to publish 11 parts, but during the process, it turned out that there was enough content for one more part. Symbolically, it became 12 parts out of 11—a classic case of an array overrun that is close to the heart of every C++ programmer :) And, of course, we couldn't break tradition (think of the "Easter egg" 51st chapter in a 50-chapter book).

So here it is: undefined behavior in all its chaotic glory. Brace yourself for an extensive read and enjoy the deep dive!

  • Part 1: introduction; what is undefined behavior and what it leads to; narrowing conversions and implicit type conversion.
  • Part 2: overflow of signed integers; floating-point numbers; integer promotion; char and sign extension.
  • Part 3: dangling references; string_view; a fly in the syntactic sugar (range-based for); self-reference; std::vector and reference invalidation.
  • Part 4: lambda function capture lists; tuples; unexpected mutability; implicit references; use-after-move; lifetime extension.
  • Part 5: Most Vexing Parse; non-constant constants; move semantics; std::enable_if_t vs. std::void_t; forgotten return.
  • Part 6: ellipsis and functions; operator []; iostreams—good luck debugging!; comma operator; function-try-block; zero-sized types.
  • Part 7: null-terminated strings; std::shared_ptr; imexplicit type conversion; how to pass a standard function and not break anything.
  • Part 8: infinite loops and halting problem; recursion; false noexcept; buffer overflow.
  • Part 9: (N)RVO vs RAII; null pointer dereferencing; static initialization order fiasco; static inline; ODR violation; reserved names.
  • Part 10: trivial types and ABI; uninitialized variables; C++20 unbounded ranges; non-virtual yet virtual functions; VLA.
  • Part 11: invalid pointers; placement new for arrays; data race; mutex deadlock; signal (un)safety; how to do everything right and trigger the deadlock.
  • Part 12: std::vector::reserve and std::vector::resize; unaligned references; time of life and death; static analysis and UB; conclusion.

Note

You can refer to this book, published as a series of articles. You can provide examples from these articles, as long as proper references are given. You must obtain the author's permission for copying or any other reproduction. Dmitry's contact: dmisvrl1@gmail.com. This content must not be used in paid services or for any fee-based teaching.

Author: Dmitry Sviridkin

Dmitry has over eight years of experience in high-performance software development in C and C++. From 2019 to 2021, Dmitry Sviridkin has been teaching Linux system programming at SPbU and C++ hands-on courses at HSE. Currently works on system and embedded development in Rust and C++ for edge servers as a Software Engineer at AWS (Cloudfront). His main area of interest is software security.

Editor: Andrey Karpov

Andrey has over 15 years of experience with static code analysis and software quality. The author of numerous articles on writing high-quality code in C++. Andrey Karpov has been honored with the Microsoft MVP award in the Developer Technologies category from 2011 to 2021. Andrey is a co-founder of the PVS-Studio project. He has long been the company's CTO and was involved in the development of the C++ analyzer core. Andrey is currently responsible for team management, personnel training, and DevRel activities.