News for hackers. Stuff that matters.
Re:Why Not Just Do Simple C++ RAII in C? by guillaumec on Thursday May 23, 2024 @03:05AM (#40451445)
I notice more and more pushes to 'improve' C and turn it into something it should not become. I feel like the C++ community gave up on C++ because of the growing complexity and so turned to C with the hope of adding to it the good parts of C++ without the ugliness. But this is of course hopeless: every added feature will create new issues that will be solved with new features until the language becomes too complex for anyone to fully understand.
Re:Why Not Just Do Simple C++ RAII in C? by fch42 on Thursday May 23, 2024 @03:21AM (#40451548)
The part in it that I don't understand is ...

Again, "traditionally", one could (ab)use C++ as "C with extras". And it wasn't uncommon, especially in resource constraint usecases, to do just that. C++ without STL or templates, or even C++ without new/delete.

This "is not C++", agree. Would a subset be enough for "using it like C-with-RAII" ?

Given the details and pitfalls the original author lists, I suspect not. It's not just C programmers who "do strange things" and make odd choices. The language itself though "lends itself to that". I've (had to) write code that sometimes-alloca'ed sometimes-malloc'ed the same thing and then "tagged" it to indicate whether it needed free() or "just" the implied drop. Another rather common antipattern is "generic embedded payloads" - the struct definition ending "char data[1]" just to be padded out by whatever creates it to whatever size (nevermind type) of that data.

Can you write _new_ C code that "does RAII" ? Probably. Just rewrite it in rust, or zig :-) Can you somehow transmogrify language, compiler, standard lib so that you can recompile existing C code, it not to "just get RAII" then at least to give you meaningful compiler errors/warnings that tell you how to change it ? I won't put money on that.

Re:Why Not Just Do Simple C++ RAII in C? by bluetomcat on Thursday May 23, 2024 @04:28AM (#40452053)
> Can you write _new_ C code that "does RAII" ? Probably.

You can do "manual" goto-based RAII in C, and it has been done for decades. The end of your function needs to have a cascading layer of labels, undoing what has been done before:

    if (!(x = create_x())) {
        goto cleanup;
    }

    if (!(y = create_y())) {
        goto cleanup_x;
    }

    if (!(z = create_z())) {
        goto cleanup_y;
    }

    do_something(x, y, z);

    cleanup_z:
    destroy_z(z);
    cleanup_y:
    destroy_y(y);
    cleanup_x:
    destroy_x(x);
    cleanup:
    return;
It just takes more discipline and is more error-prone maintenance-wise.
Re:Why Not Just Do Simple C++ RAII in C? by rcxdude on Thursday May 23, 2024 @06:25AM (#40452891)
That's not RAII, that's 'defer'. defer and context managers are both implementations of a subset of the kind of functionality you can get with RAII (the two missing parts are 1) allowing you to place an RAII object in part of a larger structure and have confidence it will actually be constructed and destructed correctly, and 2) allowing the representation of lifetimes which are more complex then just 'in this scope' via moves and copies).
Re:Why Not Just Do Simple C++ RAII in C? by gpderetta on Thursday May 23, 2024 @04:46AM (#40452192)
except this misses the point of RAII.
Re:Why Not Just Do Simple C++ RAII in C? by actionfromafar on Thursday May 23, 2024 @03:47AM (#40451767)
New/delete were never that great to begin with and have now fallen out of style. Also, C++ is quite useful and powerful even without STL.

https://www.youtube.com/watch?v=rX0ItVEVjHc

A classic which touches on such stuff.

Re:Why Not Just Do Simple C++ RAII in C? by jandrewrogers on Thursday May 23, 2024 @10:10AM (#40454975)
The STL is pretty dispensable in my experience, even for people doing full-blown modern C++, and C++20 has made that particularly obvious. The most useful feature somewhat unique to C++ is the extensive metaprogramming facility, which only recently became non-arcane.
Re:Why Not Just Do Simple C++ RAII in C? by dgellow on Thursday May 23, 2024 @05:11AM (#40452364)
Could you be more specifics? What improvements do you mean?

It’s not clear if you’re talking about defer or RAII

Re:Why Not Just Do Simple C++ RAII in C? by raydev on Thursday May 23, 2024 @11:33AM (#40455997)
> until the language becomes too complex for anyone to fully understand

Like C, with its many hidden behaviors?

Re:Why Not Just Do Simple C++ RAII in C? by tempodox on Thursday May 23, 2024 @01:22PM (#40457383)
Not hidden, the C standard spells it out. And implementation-defined behavior can be observed.
Re:Why Not Just Do Simple C++ RAII in C? by raydev on Thursday May 23, 2024 @01:47PM (#40457704)
> Not hidden, the C standard spells it out

I would argue that if it needs to be spelled out in a separate document from the code you're reading, then it's hidden.

Re:Why Not Just Do Simple C++ RAII in C? by tempodox on Tuesday May 28, 2024 @10:57AM (#40501338)
You must be joking. Like, you only use languages that don't require you to learn anything.
Re:Why Not Just Do Simple C++ RAII in C? by uecker on Thursday May 23, 2024 @06:31PM (#40460664)
What hidden behaviors? The only hidden behaviors I can think of which are somewhat problematic in C are implicit value-changing conversions. But one can instruct compilers to diagnose those.