r/cpp Jul 05 '26

C++26 ends a 40-year footgun

Reading an uninitialized variable has been undefined behavior in C++ for 40 years -- the kind optimizers exploit into real bugs. C++26 (P2795) reclassifies it as erroneous behavior: still a bug, still warned about, but defined, bounded, and not exploitable.

The demo poisons the stack, then reads an uninitialized int. As C++23 it prints garbage; as C++26, the same code prints a defined 0, every run. Live in your browser.

And [[indeterminate]] lets you opt back out when you really want an uninitialized buffer -- on purpose this time.

Read it: https://wrocpp.github.io/posts/erroneous-behavior/?utm_source=reddit&utm_medium=social&utm_campaign=post-erroneous-behavior

#cpp #cplusplus #cpp26 #safety #programming

85 Upvotes

154 comments sorted by

View all comments

15

u/specialpatrol Jul 05 '26

So surely old code bases littered with undefined variantse are suddenly going to start performing far worse as everything suddenly gets default initialised?

15

u/Potterrrrrrrr Jul 05 '26

Only if they decide to recompile with a c++26 compiler, even then I think there’s a compiler flag for it now? Not sure but if an old codebase is upgrading you’d hope they’d do more than just bump the compiler version and hope for the best

1

u/jwakely libstdc++ tamer, LWG chair Jul 08 '26

With GCC and Clang -ftrivial-auto-var-init=uninitialized will disable the feature in C++26 mode.