r/cpp • • 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

234 comments sorted by

View all comments

Show parent comment

1

u/Spartan322 Oct 16 '26

Again, violating encapsulation.

Okay cool, I really could not care less about violating encapsulation, it can be done, that's the end of it and that's my point, the dogma of encapsulation can take a hike. I sparely use encapsulation, it barely (if at all) exists in C, and too often it gets in my way in C++ to use it as anything more then the occasional tool just the same as how I use singletons and the observer pattern, its great when it doesn't get in my way, worthless when it does. Doing something well and efficient is not subscribing to one paradigm, pattern, or tool, (especially as dogma) and sometimes is actively abandoning the ones that cost you for the workload. And encapsulation is one that's certainly done that to me.

1

u/NilacTheGrim Oct 16 '26

We're arguing past each other. My argument is that this stinks, and there is no escape. Stop being stuck on the word encapsulation.

The point is that it is a code smell that this exists now and a downgrade for the language with no escape.

Arguing that you can escape it -- is just not taking into account what escaping it entails. It entails "coloring" everything that ever touches code that needs uninitialized (non-dynamic) memory.

Anyway, have a nice day.