News for hackers. Stuff that matters.
Re: by ncruces on Wednesday October 01, 2025 @04:11AM (#45435499)
> I don't understand why people insist on simulating a poor substitute for RAII with a feature that is itself almost decent RAII.

RAII doesn't make sense without initialization.

Are you proposing C should add constructors, or that C should make do without defer because it can't add constructors?

Re: by masklinn on Wednesday October 01, 2025 @05:26AM (#45435849)
> RAII doesn't make sense without initialization.

Rust has RAII and does not have constructors.

Re: by ncruces on Wednesday October 01, 2025 @06:26AM (#45436147)
Rust mandates that every field in a user-defined type is initialized at once. How do you propose to retrofit that into C without "constructors"?
Re: by dwattttt on Wednesday October 01, 2025 @11:35AM (#45438999)
C has had designated initializers since C99, if you want you can initialise every struct field at once.
Re: by ncruces on Wednesday October 01, 2025 @12:30PM (#45439719)
I encourage you to read (at least) this section of this blog before making simplistic suggestions: https://thephd.dev/just-put-raii-in-c-bro-please-bro-just-on...

How do you mandate initialization, handle copies, move objects, prevent double frees? What's RAII without any of that?

Re: by dwattttt on Wednesday October 01, 2025 @08:39PM (#45445267)
You mandate it like you mandate anything else in C. You don't.

You pick C because you want a language that doesn't require a variable to be initialised before mutably referencing it, and you write your defer statements or "destructors" defensively, expecting that a variable could be in any state when it comes time to dispose of it.

Or if you find that unacceptable, you accept that C isn't the language you want. There's many other choices available.

Re: by ncruces on Thursday October 02, 2025 @09:24AM (#45449295)
There's no way to write a "destructor" defensively, if the contents of memory it is trying to "destroy" are undefined.

Whereas it's perfectly possible to only defer a statement when you know the "object" has been properly initialized.

That's why defer makes sense in a language like C (even Go), but RAII does not.

Re: by dwattttt on Thursday October 02, 2025 @10:29AM (#45450106)
I agree with everything you've said, except the conclusion: C can't add proper safe RAII, but being "proper and safe" is not a threshold C even tries to uphold.
Re: by ncruces on Thursday October 02, 2025 @12:52PM (#45452191)
But would a destructor that runs automagically when a value goes out of scope, even if it's not properly initialized (and with zero regards for copies or moves) be in any way better than a defer that's explicitly called after it is initialized?

Cause, like, that's the entire thread.

Re: by dwattttt on Thursday October 02, 2025 @04:31PM (#45455169)
Nope, it wouldn't be. I'm just pointing out that it being unsafe or hard to use is no barrier at all to it being in the language.
Re: by masklinn on Friday October 03, 2025 @05:03AM (#45460705)
Hell, since RAII is an opt-in type-level infectious (in the sense that if you have an RAII member in a struct the struct is RAII) mechanism you can require that RAII structs be initialised.

Given the compiler would already need to know that a local is of an RAII type in order to insert the drop, move, and copy glues it’s not exactly a big requirement.