Live data from Hacker News

C: Simple Defer, Ready to Use

gustedt.wordpress.com

101–110 of 157 posts

Re: C: Simple Defer, Ready to Use

#101
post #79
post #65

Nobody knows about BOOST_SCOPE_DEFER? #include BOOST_SCOPE_DEFER [&] { close(fd); }; Implementation here: https://github.com/boostorg/scope/blob/develop/include/boost...

Only for those willing to stomach the entire boost hairball, which in embedded environments might be too much to ask.

Is that why I got down voted? This thing is header-only, I'm guessing it doesn't need rtti or have a big footprint.

Re: C: Simple Defer, Ready to Use

#102
post #27

Does C really need this? Do languages need to grow in this way? The overriding virtue of C is simplicity.

> Does C really need this? Yes. > Do languages need to grow in this way? Yes. > The overriding virtue of C is simplicity. C is not simple. It used to be, but it's not been for a long time.

About your last point : https://queue.acm.org/detail.cfm?id=3212479

Re: C: Simple Defer, Ready to Use

#104

I don't think "you can implement this slightly modified version of the proposal in GCC macros" is a good reason to change the defer proposal. It doesn't work in clang¹ or any other non-GCC C implementation, a trailing semicolon after the close brace is an ugly wart, and saying "if you want to implement this all you have to do is implement [[gnu::cleanup]] and nested local functions" is presumably much more work for o…

I don't think they're advocating not doing defer in C? They're saying you can backport the functionality if needed, or if you want to start using it now.

Re: C: Simple Defer, Ready to Use

#105
One can use C's `cleanup` attribute like a defer. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute...

The function signature is `void(MyType*)` if it is used like:

    __attribute__((cleanup(MyTypeCleanupFunction)))
    MyType myType;
If it's a pointer, it'll call the cleanup as a double ptr, and etc. Note that the specified cleanup function can do anything you want to, though the intended purpose is to cleanup the variable when it goes out of scope.

Re: C: Simple Defer, Ready to Use

#106

One can use C's `cleanup` attribute like a defer. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute... The function signature is `void(MyType*)` if it is used like: __attribute__((cleanup(MyTypeCleanupFunction))) MyType myType; If it's a pointer, it'll call the cleanup as a double ptr, and etc. Note that the specified cleanup function can do anything you want to, though the intended purpose is to cleanup t…

That isn't a C feature, it's a feature of GCC and other compilers. While I have used (and horribly overused) `cleanup` in personal projects, it's not a replacement for a feature being in the standard.

Re: C: Simple Defer, Ready to Use

#108
post #22

Earlier quoted context omitted.

I use a few strategies instead of goto: (1) For simpler cases, wrap in do {} while (0) and break from the loop (2) For multiple cleanups, use same technique combined with checks to see if the cleanup is required. E.g. if (f != null) fclose (f) (3) put the rest of the stuff in another function so that the exit code must run on the way out. In 35 years of coding C/C++, I've literally never resorted to goto. While conve…

35 years of coding C don't amount to much if you ignore best practices. Look at the Linux kernel to learn how gotos can be used to make the code safer and improve clarity. There's probably something wrong if a substantial project in C does NOT use gotos.

Would any particular piece of the kernel stand out to you in this manner? Last time I looked I didn't think too much about it being used.

Re: C: Simple Defer, Ready to Use

#109
post #22

Very nice, using the goto trick to perform cleanups always felt dirty to me.

I use a few strategies instead of goto: (1) For simpler cases, wrap in do {} while (0) and break from the loop (2) For multiple cleanups, use same technique combined with checks to see if the cleanup is required. E.g. if (f != null) fclose (f) (3) put the rest of the stuff in another function so that the exit code must run on the way out. In 35 years of coding C/C++, I've literally never resorted to goto. While conve…

How do you handle breaking out of a nested for loop without goto?

    for (…) {
        for (…) {
            if (…) {
                …
                goto found;
            }
        }
    }
    found:
This is straightforward with goto and may even be vectorizable. I guess you could move the loop to a separate function or add additional flags to each loop, but neither of these seems like an improvement.

Re: C: Simple Defer, Ready to Use

#110
post #14

Earlier quoted context omitted.

Dijkstra was not wrong. Modern programmers are wrong in thinking that the goto that they use is what Dijkstra was talking about, merely because of the fact it happens to be called the same thing. I mean, I get how that can happen, no sarcasm, but the goto Dijkstra was talking about and what is in a modern language is not the same. https://jerf.org/iri/post/2024/goto/ The goto Dijkstra is talking about is dead. It liv…

Your analysis is wrong. Dijkstra was a big proponent of structured programming, and the fundamental thesis of his argument is that the regular control flow structures we're used to--if statements, loops, etc.--all represent a tree-based data structure. In essence, the core argument is that structured programming allows you to mentally replace large blocks of code with black boxes whose exact meanings may not be impor…

I struggle with how you claim my "analysis is wrong", and then basically reiterate my entire point. I know it's not that badly written; other people got it just fine and complain about what it actually does say.

The modern goto is not the one he wrote about. It is tamed and fits into the structured programming paradigm. Thus, ranting about goto as if it is still the 1960s is a pointless waste of time. Moreover, even if it does let you occasionally violate structured programming in the highly restricted function... so what? Wrecking one function is no big deal, and generally used when it is desirable that a given function not be structured programming. Structured programming, as nice as it is, is not the only useful paradigm. In particular state machines and goto go together very nicely, where the "state machine" provides the binding paradigm for the function rather than structured programming. It is perhaps arguably the distinctive use case that it lives on for in modern languages.

Post reply on HN