r/cpp • u/Clean-Upstairs-8481 • Sep 27 '26
You should think about recompiling your C++ programs with GCC 16 and C++26, because it zero-fills your locals
https://techfortalk.co.uk/2026/09/27/cxx26-uninitialized-local-variables-gcc-16/Stack variables are not automatically initialised, and that is the root cause of many C++ bugs. That is well known. Hence, it is advised that local or stack variables are always initialised with known values, 0 if not something more meaningful than that. Now, with GCC 16 compiling in C++26 mode (-std=c++26), even uninitialised local variables will be zero-filled. In the post, I have explained how.
270
Upvotes
View all comments
45
u/mark_99 Sep 27 '26
What you’re observing is C++26 “erroneous behaviour “ combined with implementation-defined behaviour for that particular compiler.
There is no guaranteed zero fill, nor should there be as would create a dialect where code snippets would be UB in older C++ or without compiler-specific flags. Reading a zero wouldn’t necessarily be better, and would in fact be worse in that if it was defined you couldn’t get a diagnostic.
Reading uninitialised values is still an error and you’ll get a best-effort diagnostic if the compiler can statically analyse the read-before-write.
EB keeps the bad thing as incorrect, but stops it being UB and the unpredictable side effects that can cause under optimisation.
As a rule disassembling what the compiler happens to do isn’t “proof” - it’s important to look at the actual language standard.
> Instead of default initialization, with erroneous behaviour, an uninitialized object will be initialized to an implementation-specific value. Reading such a value is a conceptual error that is recommended and encouraged to be diagnosed by the compiler. That might happen through warnings, run-time errors, etc.
https://isocpp.org/blog/2025/05/cpp26-erroneous-behaviour-sandor-dargo1