Felix' Ramblings
<< The Sad State of Development Tools and Features
>> I Hate: Everything?!

2023.12.04
C++ People Must Be Trolling

Someone on Twitch mentioned this C++ Committee insanity, and I feel like I'm going to explode not writing this down. I already posted a wall of text to two friends of mine, so I might as well save this here.

The Proposal

Someone did an official proposal to the C++ specification to zero-initialize memory of all objects using "automatic storage duration". To the best of my knowledge, this means (non-static) variables on the stack. In short:

We propose to zero-initialize all objects of automatic storage duration, making C++ safer by default.
Source

which means:


void TestFunction(void)
{
	// Before:
	{
		int y;     // y is not initialized
		int x = y; // Reading from y is undefined behaviour, let there be dragons!
		// ...
	}

	// After
	{
		int y;     // is equivalent to "int y = 0;"
		int x = y; // Well defined, x has the value 0.
		// ...
	}
}

I'm seeing less undefined behaviour and less having to type = 0 -> I like.

Even better, they already did performance benchmarks, and the results seem pretty reasonable: Some projects showed a performance decrease of up to 0.5%, and some projects have seen a performance increase of up to 1.0%. And for new code, they introduced a new attribute such that you can explicitly ask for uninitialized memory. This should help mitigate a few performance regressions when working with e.g. big structs or something [0].

So this is the part I am be pleasantly suprised that C++ implemented some thing simple and nice. I'm gonna give this a thumbs up and hope that this behaviour is ported to C in some future revision, right? This change seems very simple and uncontroversial. Surely people see the benefit of this proposal, right?

Right, guys?

The Other Proposal

You know how the C (and especially the C++) spec is clusterfuck of

I already have to look up the differences between these three "behaviours" too often. You know what C++ needs?

That's right, "Erraneous Behaviour" (EB for short), obviously. And not only that, reading from an uninitialized local variable should be EB.

...What does that mean? Well, in this specific instance, it means:

Ok, time to take a breather. This shit does not makes sense to me.

It's just a proposal. No need to be upset. This shit will be discarded and the other one accepted. Surely. Let's look at the proposal progress [1]:

The proposal for zero-initialized objects was last updated / discussed in February. Is that recent? I don't know, I don't have context for this. This could be fine, and the issue even states that they are interested in pushing through some form of automatic stack variable initialization. Sounds good I guess? Not much discussion around it and no indicator on whether it's accepted into the next standard, but maybe that comes in the future?

The proposal for EB was posted in February. Oh oh. It was last updated / discussed two weeks ago. Oh no. They don't want to pull in the proposals as is, but they want to extend the amount of cases in which EB is applied. Oh fuck no.

Again, the proposal is not yet accepted either, but it sure does not seem to be discarded at all. But I have ommitted a little detail: What do you think is the argument against initializing variable to zero by default?

It's not performance.

Oh no, it's a much better argument.

Wait for it...

IT'S "UNDERSTANDABILITY":

"The resulting behaviour is desirable, but the cost on code understandability is unacceptable to the present author."
Source

They, in the context of C++, are arguing, that


int b; // is zero
is unacceptable because it makes code harder to understand????????? They have to be trolling.

I refuse to believe this.

They shot this idea down, because of understandability, and then decided to introduce the notion of "Erraneous Behaviour".

The C Standard

Good news: I probably don't have to worry about C introducing this kind of shit.

Bad news: It's because they generally dislike the idea of automatic zero-initialization in this context.

Jai pls save me


[0]: Coming from C, I don't really use attributes. I'd prefer something like Jai where you can assign the "undefined" value --- to get uninitialized memory, but this is personal preference.

[1]: To give props here: I think it's pretty neat the status of proposals are public on github.


<< The Sad State of Development Tools and Features
>> I Hate: Everything?!
 Felix' Ramblings