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

3

u/UndefinedDefined Sep 27 '26

Is this the result of the genius "Let's make C++ slower" campaign?

5

u/Hedede Sep 28 '26

Do you even know what the performance cost of zero-initialisation is?

-4

u/UndefinedDefined Sep 28 '26 edited Sep 28 '26

Of course - I cannot imagine a compiler zero initializing my temporary arrays! But there is something even worse here. It makes a difference between local vs heap allocations. Genius idea. Let's make locals zero initialized, but heap memory not and learn another rule. There are not enough rules in C++, we need more of them, contradicting each other is the goal.

Just try this nice godbolt: https://godbolt.org/z/oa1e6aobd

And now why would C++ need this? Just use golang if you need zero initialization - it would zero initialize all the memory it gives you, and as a bonus you will be writing object pools to avoid it.

1

u/Hedede Sep 29 '26 edited Sep 29 '26

Just try this nice godbolt: https://godbolt.org/z/oa1e6aobd

The behavior is unchanged whenever I change C++ standard version and/or compiler version.

x86-64 clang 23.1.0

Execution build compiler returned: 0
Program returned: 0
Garbage dynamic 0 static=1425874296

x86-64 clang 18.1.0

Execution build compiler returned: 0
Program returned: 0
Garbage dynamic 0 static=-184668717

x86-64 gcc 16.2

Execution build compiler returned: 0
Program returned: 0
Garbage dynamic 235706 static=0

x86-64 gcc 12.1

Execution build compiler returned: 0
Program returned: 0
Garbage dynamic 45188 static=0

1

u/UndefinedDefined Sep 29 '26

Look at the assembly, just because the register happens to be zero you get zero.

You can try something more elaborate, like this https://godbolt.org/z/6jfzshMjo but why? The language will have this, so I guess it's finally time to create a C++ standard version ceiling in my projects as I see no point in reafactoring code that has been already working for more than a decade. And I don't want more macros in my code or to increase the standard to C++26 just to use [[uninitialized]] or whatever name it's now.

2

u/Hedede Sep 29 '26 edited Sep 29 '26

Look at the assembly, just because the register happens to be zero you get zero.

gcc 12.1 -std=c++20

garbage_prepare():
    sub     rsp, 8
    mov     edi, 4
    call    operator new(unsigned long)
    mov     esi, 4
    mov     DWORD PTR [rax], 12648430
    mov     rdi, rax
    add     rsp, 8
    jmp     operator delete(void*, unsigned long)
garbage_dynamic():
    push    rbx
    mov     edi, 4
    call    operator new(unsigned long)
    mov     esi, 4
    mov     ebx, DWORD PTR [rax]
    mov     rdi, rax
    call    operator delete(void*, unsigned long)
    mov     eax, ebx
    pop     rbx
    ret
garbage_static():
    xor     eax, eax
    ret
.LC0:
    .string "Garbage dynamic %d static=%d\n"
main:
    sub     rsp, 8
    call    garbage_prepare()
    call    garbage_dynamic()
    mov     edi, OFFSET FLAT:.LC0
    mov     esi, eax
    call    garbage_static()
    mov     edx, eax
    xor     eax, eax
    call    printf
    xor     eax, eax
    add     rsp, 8
    ret

gcc 16.2 -std=c++26

"garbage_prepare()":
    sub     rsp, 8
    mov     edi, 4
    call    "operator new(unsigned long)"
    mov     esi, 4
    mov     DWORD PTR [rax], 12648430
    mov     rdi, rax
    add     rsp, 8
    jmp     "operator delete(void*, unsigned long)"
"garbage_dynamic()":
    push    rbx
    mov     edi, 4
    call    "operator new(unsigned long)"
    mov     esi, 4
    mov     ebx, DWORD PTR [rax]
    mov     rdi, rax
    call    "operator delete(void*, unsigned long)"
    mov     eax, ebx
    pop     rbx
    ret
"garbage_static()":
    xor     eax, eax
    ret
.LC0:
    .string "Garbage dynamic %d static=%d\n"
"main":
    sub     rsp, 8
    call    "garbage_prepare()"
    call    "garbage_dynamic()"
    mov     edi, OFFSET FLAT:.LC0
    mov     esi, eax
    call    "garbage_static()"
    mov     edx, eax
    xor     eax, eax
    call    "printf"
    xor     eax, eax
    add     rsp, 8
    ret

See any difference? Me neither.

And I don't want more macros in my code or to increase the standard to C++26 just to use [[uninitialized]] or whatever name it's now.

You can put [[indeterminate]] in your code without using macros or bumping the standard.

2

u/UndefinedDefined Sep 29 '26

You need to use the second version with array, to see a call to memset - thank me later, long live golang...

1

u/Hedede Sep 30 '26

Sure, but you posted your first one as an "example of another inconsistency in C++26" when it wasn't.

-1

u/UndefinedDefined Sep 30 '26

Of course it was, you weren't just looking. But it's okay, not everybody can see it.

-1

u/UndefinedDefined Sep 29 '26

warning: unknown attribute 'indeterminate' ignored [-Wunknown-attributes] - enabled automatically by clang, for example. Great to see warnings again!

2

u/Hedede Sep 30 '26

-Wno-unknown-attributes, why do you have to make a problem out of everything

-1

u/UndefinedDefined Sep 30 '26

Genius idea! And enforce this on all the users using your library to make their lives suck more!

3

u/pjmlp Sep 28 '26

More like "lets take C++ out of the unsafe languages" governments lists campaign.

0

u/UndefinedDefined Sep 28 '26

I'm honestly not sure how this solves the safety problems. It only introduces more problems to existing code that expects that temporaries are without overhead. Maybe I should not leave C++20 when I think of it. It's a good standard, I avoid like 80% of the standard library and I cannot be happier about it at the moment. But this "genius" idea just makes my life harder. Uninitialized locals are trivial to find, so in the end this helps with nothing. And I'm not really sure why everybody should pay price just because there is broken code in the wild, written without tests and without any serious CI.