Live data from Hacker News

C: Simple Defer, Ready to Use

gustedt.wordpress.com

141–150 of 157 posts

Re: C: Simple Defer, Ready to Use

#141

Earlier quoted context omitted.

> cleanup in every single failure case you're only tempted to clean up [fully] in every single failure case if you don't know how to implement cascading gotos or the arrow pattern.

I'll be honest that you are in the minority here. goto is widely considered an anti-pattern and so is the arrow pattern.

> you are in the minority here

Yes, I am.

Re: C: Simple Defer, Ready to Use

#142
post #25

The C++ lambda version can be a foot gun: https://godbolt.org/z/Wd66GcrdG , if the return value is a struct, NRVO (named return value optimization) may be applied and the lambda will be called in different order. As for the n3434 proposal, given that the listed implementation experiences are all macro-based, wouldn't it be more easily adopted if proposed as a standard macro like ?

template struct __df_st : T { [[gnu::always_inline]] inline __df_st(T g) : T(g) { // empty } [[gnu::always_inline]] inline ~__df_st() { T::operator()(); } }; #define __DEFER__(V) __df_st const V = [&](void)->void #define defer __DEFER(__COUNTER__) #define __DEFER(N) __DEFER_(N) #define __DEFER_(N) __DEFER__(__DEFER_VARIABLE_ ## N) #include struct S { int r; ~S(){} }; (i know hn will mangle this but i won't indent thi…

That's from the OP blog, the intention of those macros is to generate uniquely named local lambda.

Cleaner version is like: https://github.com/llvm/llvm-project/issues/100869#issue-243...

Re: C: Simple Defer, Ready to Use

#143

Earlier quoted context omitted.

Ah, I forgot about -fexceptions, Thanks. Though I need to point out it's non-default and rarely used, and in particular: > some C libraries need to enable it anyway if they want to interact with C++ exceptions this way. For example C++ requires that qsort and bsearch propagate the exception thrown by the comparison callback normally -fexceptions is not needed for this, C++ exceptions will just transparently bubble th…

glibc allocates in its fallback algorithm within qsort. It has to free the allocation when a C++ exception is thrown in a comparison, unless it wants to leak. I recently filed a ticket for this. They kinda have to compile at least qsort with -fexceptions, unless they use a fully in-place sorting algorithm.

Oof, what an ugly issue… I kinda thought (hoped?) it'd be in-place anyway and thus not need any heap interactions, but I guess it does and they have no leeway to change the (fallback) algorithm…

It's a bit weird for the C++ standard to require behavior off C standard functions though, I gotta say… seems more sensible to use a C++ sort…

Re: C: Simple Defer, Ready to Use

#144
post #55
post #43

Earlier quoted context omitted.

Fair, I was not explaining why the statement was wrong. Anyhow, the trampoline is not related to the call site being in the same function but whether a pointer is generated to the nested function and escapes. Nested functions in general do not need trampolines or executable stack.

And I should mention that GCC nowadays can also allocate the trampoline on the heap: -ftrampoline-impl=heap

Well, the heap is also non-executable on a whole bunch of platforms these days (and for good measure, w^x is a good concept to go by; even JITed code shouldn't be writable and executable at the same time)

Re: C: Simple Defer, Ready to Use

#145
post #73

I once worked on a student robotics project where if you didn't gracefully shut down the connection to a specialized camera, it had a significant chance of physically bricking the camera. We were using C++ and essentially instrumented code review processes, despite being a student group, to ensure nothing was ever called in a way where the destructor wouldn't be called - and broke out vision processing into a separat…

Yikes. So the OOM Killer bricks your camera then!?!

Just (carefully) power down the robot before the memory leaks exceed available RAM!

Re: C: Simple Defer, Ready to Use

#146
post #98
post #74

Earlier quoted context omitted.

I've used nested functions for a very long time with gcc and they are completely reliable. Since my code is embedded without an MMU the oh noes executable stack doesn't fill me with any dread. It's unfortunate a lot of the standards guys are horrified by anything that isn't C89. Because if the executable stack is an issue it's worth fixing. Side note: 20 years ago people thought if they made the stack non executable…

> Side note: 20 years ago people thought if they made the stack non executable that would totally fix stack smashing attacks and unfortunately it only slows down script kiddies. Slowing them down is good. And: separating data and code helps simplify managing the caches.

One could easily remove the need for trampolines by having a wide function pointer type. In C, this would even be allowed without having a new type, but ABI compatibility makes this not practical. With a new qualifier this would be no problem though. The real reason some people are against it is that it diverges from C++.

Re: C: Simple Defer, Ready to Use

#147
post #73

I once worked on a student robotics project where if you didn't gracefully shut down the connection to a specialized camera, it had a significant chance of physically bricking the camera. We were using C++ and essentially instrumented code review processes, despite being a student group, to ensure nothing was ever called in a way where the destructor wouldn't be called - and broke out vision processing into a separat…

Yikes. What is it with high school robotics and buggy camera APIs? Back when I competed, we were using Java with the FRC provided vision library. When we tried adding the camera, we discovered that our Java code started to segfault after a few minutes of running. This turned out to be comming from the native camera code being invoked through the FFI.

This wasn't as bad as what you saw, as a restart would fix it (for a few minutes at least), but would disable us for the rest of a match if it ever happened at a competition.

At the end of the day, we worked around this by implementing a null pointer check. It was something along the lines if:

    if (camera.toString().contains("(null)") return;
I assume there was still a race condition where the garbage collector would trigger whatever ffi destructor nulled out the relevent pointers; but we never ran into it.

To your point, this looks to be an alternative to the 'goto end' pattern in C. In my experience that is the least bug prone pattern of destructors for C code.

Re: C: Simple Defer, Ready to Use

#148
post #27

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

C definitely does not need this - in fact I would go as far and consider it harmful.

I agree that a feature like this could be useful, but then there are other useful features which should be added too. Where do you stop? I hope the C standards committee does not succumb to the feature bloat trend we see in many other languages (hand on heart: how many people fully understand/master all of C++/23's features?).

Need proper arrays instead of pointers to contiguous parts of memory which are interpreted as arrays? Proper strings (with unicode)? Etc. - use a different language suitable for the job.

We really need to let go of the notion that complex software must be written in a single language. Use C for the low level stuff where you do not need/want an assembler or where you want full control. For everything else there are more suitable tools.

Re: C: Simple Defer, Ready to Use

#149
Everyone that doesn't like a new standardized defer is doing https://knowyourmeme.com/memes/no-take-only-throw but as "no forest, only trees"

The explicit alternative for defer is an unreadable mess. And guess what? If you really need to see it, we should having tooling that desugars the defer to gotos. That's the best of both worlds!

- The "master" source code is high level "say what I mean"

- Low level view that is still much higher than dropping down into assembly still exists!

You really should be able to express the required cleanup semantics with "defer", and if you cannot, you can always just replace the original with the low level view and edit it instead --- good luck getting that past code review, however :).

Re: C: Simple Defer, Ready to Use

#150
This is completely unnecessary. Just use a goto statement with labels. The whole point of C is that it's a simple language, and adding more complexity like 'defer' just makes it harder to understand. This proposal is just making C more like those bloated modern languages. If you need fancy features like this, use Rust instead.
Post reply on HN