23 ms·
A simple defer feature for C
- xayfs 5y ago
- aaaaaaaaaaab 5y agoJust use C++. No need to proliferate the C language.
- neatze 5y agoI highly doubt you can explain why.
- kaba0 5y agoNot the parent commenter, but namespaces alone make the switch a no-brainer. Add on top RAII, actual abstractibility (good luck implementing std::string in C properly) make it a strictly worse language.
- daneelsan 5y agoWhy ado you think namespaces make it a no brainer? I would argue the only thing I would want from C++ it's operator overloading, and I'm not even conviced about that.
- jcelerier 5y agoTook a few years for C to adopt function prototypes from C++, one can hope that RAII makes it in less than a century
- thechao 5y agoI hope it's not mandatory. I'd much prefer static analysis to tell me of use-before-acquire errors: I have a number of hot-inner-loops where initialization nukes performance.
- edflsafoiewq 5y agoCan you give an example of how initialization nukes performance?
- thechao 5y agoIn the context I'm thinking of, I have fairly sizable arrays -- up to 512 bytes; usually, only the first few bytes are used (for state tracking); an `int` tells me the high-water mark so the data is only read after being written. The amount of work int the loop is (in almost all cases) only 5 or 6 instructions before termination. Initializing 512 bytes is best case 8 ops, which is >100% overhead. The code is recursive, but bounded to a depth of 8, wity internal linkage (only two `int`s and a pointer are passed) in the tail call position, so the calling convention is just three registers. Even a compiler like q9x, or clang with no opts, produces excellent code. But, even a sniff of initialization tanks perf to the tune of 3x.
- jcelerier 5y ago> Initializing 512 bytes RAII in C++ does not mean that all bytes are initialized, only the ones you want. In struct Foo { int header = 0; float foo; int data[64]; int footer; // just to show that having an explicit constructor / dtor does not change anything Foo(): footer{456} { } ~Foo() { } }; only "header" and "footer" will be initialized: https://gcc.godbolt.org/z/4W1WzfPEv https://gcc.godbolt.org/z/4W1WzfPEv It's not a matter of optimization, absolutely no compiler will zero-initialize unless you explicitely ask for it (or you are in a situation where the language mandates initialization, like global static in C and C++)
- dataangel 5y ago> It's not a matter of optimization, absolutely no compiler will zero-initialize unless you explicitely ask for it Oh man I'm about to ruin your day. Behold the horror: https://stackoverflow.com/questions/29765961/default-value-and-zero-initialization-mess https://stackoverflow.com/questions/29765961/default-value-a...
- jibalt 5y agoPrototypes were in the original C standard of 1985, before first edition of The C++ Programming Language was released and long before C++ was formalized standardized.
- Sindisil 5y agoOy. Your history is ... confused. While the first draft was released in 1983, the first ratified C standard was in 1989. The first edition of "The C Programming Language" (from which I first learned the language) was published in 1978, over a decade before the language was standardized. You are correct that C89 added function prototypes. However, contrary to your implication, C borrowed the concept and syntax from C++.
- jibalt 5y agoEverything I said was correct. I was a member of X3J11 (which was established in summer 1983), the C language standard, and was the first person on the planet to vote to approve it (due to alphabetical order) ... that was in 1985. It was several years before it was ratified, the delay having to do with standardization politics which required a lot of buy-in. I have no idea why you're mentioning the first edition of K&R from years earlier, which of course did not have prototypes. There's a notable difference between C's prototypes and C++'s ... `int foo();` in C is not a prototype, whereas in C++ it's equivalent to `int foo(void);` ... C had to maintain compatibility with K&R style declarations, whereas C++ didn't.
- ohCh6zos 5y agoDefer is a bad feature in Go and I'm hesitant to see it spread to other languages. It exists as a hack because error handling in Go is convoluted.
- leni536 5y agoDefer is weird in Go, being tied to function scope instead of immediate block scope.
- ohCh6zos 5y agoYou're right, that is a better way to put it than what I said.
- assbuttbuttass 5y agoFunction scope allows constructs like for _, lock := range locks { lock.Lock() defer lock.Unlock() } To acquire locks in a loop and release them at the end of a function. Otherwise the previous lock would be dropped each time the loop executes. I've never seen the advantage to having a block structured defer: it's easy to add a new function, it's not always possible to remove a block.
- jcelerier 5y ago> To acquire locks in a loop and release them at the end of a function. that looks like a super big gotcha, hasn't there been enough bugs with alloca() being called in a loop yet to show how risky and unintuitive this is ? block-scoped is explicit and explicit is good
- kaba0 5y agoGood implementations of defer are scope-based, so those would be clean up at the end of the loop pretty explicitly.
- duped 5y agoNamed scopes would alleviate this problems, and I wish more languages would adopt them.
- marcodiego 5y agoThe new defer proposal has the potential to make code easier to read, remove some uses of goto's and help to fix some resource leaks that are so common on old non-GC languages. Certainly a feature I'm rooting for. The only other proposal I'm more interested in are the lambda approaches: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2890.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2890.pdf
- rwmj 5y agoUgh please don't do this. Just standardize the existing __attribute__((cleanup)) mechanism which is already implemented by compilers and widely used in many free software projects.
- kps 5y ago“this feature cannot be easily lifted into C23 as a standard attribute, because the cleanup feature clearly changes the semantics of a program and can thus not be ignored.”
- nmilo 5y agoThen it doesn't need to use the existing attribute syntax, which requires that attributes can be ignored. They can make a new syntax.
- rwmj 5y agoThat's a very weak objection. In any case since most programs are hiding __attribute__((cleanup)) in a macro (eg. glib's g_autoptr macro or systemd's family of _cleanup_* macros) you could use another kind of annotation. The point here is that the C working group should be standardizing existing practice and helping the existing users of C, and not striking out making bizarre new syntax choices and keywords which are completely unproven.
- jibalt 5y agoSo don't lift it as a standard attribute.
- deleted 5y ago[deleted]
- 10000truths 5y ago__attribute__((cleanup)) is ugly because for some reason, the gcc folks decided that the parameter passed to cleanup needed to be a function pointer of signature void(void**) instead of the much more sane void(void*), with no way to allow implicit casting of the parameter to a function pointer of different type, so none of the basic cleanup functions (e.g. free, fclose) work with it out of the box - you need to pollute your codebase with one wrapper for each cleanup function that you want to use. They should have made it so that cleanup took an expression block: void* foo __attribute__((cleanup({ free(foo); }))) = malloc(bar); Or at least allow a statement expression returning a compile-time known function pointer: void* foo __attribute__((cleanup(({ void wrapper(void** p) { free(*p); }; wrapper })))) = malloc(bar); Sure, it still looks ugly, but at least a single generic macro would be able to clean it up and make it look better.
- nmilo 5y agoI really don't like it. The lambda stuff, especially the captures that were ripped from C++, don't fit C at all. The question about whether it should be cleaned up at end-of-scope or end-of-function is too ambiguous and debated. And if I'm understanding this correctly, this is the worst part: > This indicates that existing mechanism in compilers may have difficulties with a block model. So we only require it to be implemented for function bodies and make it implementation-defined if it is also offered for internal blocks. How will that work?
- jimjams 5y agoSince variables can be scoped to blocks, and allocation scopes can be arbitrary, needing neither a function or block scope to bracket them, I don't see how that will fly either.
- massinstall 5y agoI agree and don’t like it either. +1
- bangonkeyboard 5y agoI was, and continue to be, very disappointed that the (to me) more natural and C-like block syntax for lambdas was rejected from C11 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2030.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2030.pdf).
- b3morales 5y agoIn practice those signatures can get a bit tiresome to write out, but I agree, they fit much, much better with C syntax and mindset.
- bangonkeyboard 5y agoCertainly, but no more tiresome than the existing C syntax for function pointers, to which they were direct analogues.
- cyco130 5y agoIsn't this scope guard: https://tour.dlang.org/tour/en/gems/scope-guards https://tour.dlang.org/tour/en/gems/scope-guards ?
- david2ndaccount 5y agoI usually use the single-exit-point idiom with gotos to handle cleanup. I now wonder if there is a compiler that helps you enforce that so you don’t randomly stick a return in the middle of your function. Anyway, this would be great to have in C as it would simplify how resources are handled.
- 95014_refugee 5y agoYou can use the cleanup attribute and a flag to detect early exit.
- loeg 5y agoYou can use the non-standard, but typically implemented, cleanup attribute today for function-scoped cleanup. I don’t know if compilers inline this kind of “destructor”. It works well — I’ve seen it used successfully at multiple employers.
- greenn 5y agoIf the purpose of defer is to replace the `goto single_exit`, I think the value captures are unnecessary. Any single_exit I've implemented uses the value at the time of function exit, so that I can realloc in the middle of the function. It would be a shame if this defer was limited to function scope. It would be very useful in nested blocks as well. But, I would still appreciate it. defer and auto are the only things I would love to see in C.
- lerno 5y agoI don't mind a sane defer in C, that is something which follows scope in the same way stack allocated destructors are invoked. This follows the behaviour in other languages like Swift. Why "on function exit" style defers - already known to be a bad idea from Go - is beyond me. Such a solution more or less requires storage of potentially unbounded allocations. Foo *f; for (int i = 0; i < very_big_value; i++) { f = get_foo(i); // BOOM defer return_foo(f); } I would sort of understand if this was proposed for a high level language garbage collection where actual memory usage is secondary.
- cpuguy83 5y agoIn this case you wrap the loop in an anonymous function if you need it to be cleaned up within the scope of the loop. Or move the functionality to another function.
- knome 5y agoAdding a statement that would require a dynamic allocation for every iteration of a loop is kind of insane for C. It doesn't matter what the defer does, if it's allocated in a for statement and doesn't go off until the surrounding function exits, then it's going to have to stuff tons of function pointers and parameters somewhere, presumably alloca'd onto the stack over and over. That's not C. That shouldn't be C. There are a dozen languages for doing clever dynamic magical things in code. C is still fairly straightforward. Use one of the other languages instead of bagging down C with complexity until it turns into another difficult C++ variant over time.
- adamrt 5y ago> Why "on function exit" style defers - already known to be a bad idea from Go - is beyond me Is there something you can point me to about this? I write Go professionally and from a readability and utility standpoint I really like it in common scenarios. I hadn't heard its a know bad idea and am just curious. Thanks.
- kaba0 5y ago
- pornel 5y agoSince Microsoft has decided to sabotage C by not implementing anything that isn't in C++ already, and this will never be in C++, this feature is already dead. Projects that target only GCC and Clang can use __attribute__((cleanup)) without waiting a decade for it.
- xeeeeeeeeeeenu 5y ago> Since Microsoft has decided to sabotage C by not implementing anything that isn't in C++ already, and this will never be in C++, this feature is already dead. This is no longer true: https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...
- frankohn 5y agoYes it is still true, they just have decided to support newer c standards when they sabotaged c11 by making optional a fundamental feature like complex numbers: > Support for Complex numbers is currently not planned and their absence is enforced with the proper feature test macros. Sadly, there is no future for the C language with a corporation like them in the committee. Much better to look at Zig or Nim, they fill more or less the same space and are developed by smart and passionate people.
- pjmlp 5y ago> A compiler that defines __STDC_IEC_559_COMPLEX__ is recommended, but not required to support imaginary numbers. POSIX recommends checking if the macro _Imaginary_I is defined to identify imaginary number support. They have not sabotaged anything when the feature is optional to start with. If ISO wanted everyone to actually support it, it wouldn't be optional.
- emptybits 5y agoI'm interested. So defer is one of the handful of achievable goals I find Zig interesting and hold some hope for it. Easy to call C from Zig or Zig from C. And Zig can also compile C and target cross-platform easily. const sprite_sheet_surface = c.SDL_LoadBMP("res/tile.bmp") orelse { c.SDL_Log("Unable to create BMP surface from file: %s", c.SDL_GetError()); return error.SDLInitializationFailed; }; defer c.SDL_FreeSurface(sprite_sheet_surface);
- fshee 5y agoSomewhat related: I hacked some macro up in gnu89 C to get a Go styled defer once upon a time. I felt bad for making my compiler course instructor review that code... Very convenient, though.
- eqvinox 5y ago> Whereas gcc’s cleanup attribute is attached to functions, POSIX’ cleanup functions and the try/finally are attached to possibly nested blocks. > This indicates that existing mechanism in compilers may have difficulties with a block model. Erm. No. GCC's cleanup attribute is attached to nested blocks. Whatever that means for the conclusion there. How is a pretty basic misunderstanding like this seeping into an ISO WG document?!? [add.:] in case anyone wants to double check: https://gist.github.com/eqvinox/c062f5a46f3a60b1151fcdf3e91a6da0 https://gist.github.com/eqvinox/c062f5a46f3a60b1151fcdf3e91a...
- tialaramex 5y agoAn individual or handful of people make a proposal, the Working Group looks at the proposal, maybe it gets revised (this is version 2). Working Groups themselves do not, on the whole, produce documents like this. There is no ISO magic ensuring working groups are all-knowing. There's an excellent chance that no members of WG14 have a deep in-depth knowledge of GCC, or that members who did they weren't particularly interested in that part of this paper (e.g. they were already staunchly for or against, remember ISO Working Groups are democratic, a proposal does not need consensus, so a working group member who thinks your idea is inherently good or bad might just skim the introduction, and move on)
- UltraViolence 5y agoC should be retired and replaced with compiled C# (if the usage allows for Garbage Collection) or Rust (if deterministic memory management is needed). There's really no use for pulling on a dead horse. C has had its heyday and should simply curl up and die.
- Shadonototra 5y agothis is the worst idea i read today, thanks people don't understand what "programming" is about anymore
- rsj_hn 5y agoI think one difference between a classically trained programmer of a few decades ago and many of the programmers today who entered from javascript or bootcamps or were even self-taught is lack of understanding about all those other systems below you. For example, do you think the OP has heard of Simple Managed C? C# is great, but it's not a systems language, it depends on piles of C/C++/etc code in order to run.
- sharikous 5y agoTo be able to program in C# you don't need any .c file in your whole computer. Of course you need a lot of binaries which were produced somewhere using low level languages in the process, and you probably need to comply with the C FFI to access a lot of libraries. But nothing that cannot be done with a different low level language.
- rsj_hn 5y agoYou really on an entire stack that is programmed and maintained in languages like C, and to the degree that these are provided for whatever chipset you are using, yes you can code in C++. And of course you expect these libs to be regularly patched and updated, and released as new platforms become available, etc. I'm not saying "don't code in higher level languages". I'm saying that not everyone can code in higher level languages. There is a whole stack that needs maintenance and development.
- nrabulinski 5y ago“Simple” defer feature - proceeds to implement c++ closures and semantics just to have a defer keyword which invokes them at the exit point of a scope
- Shadonototra 5y agoi prefer D's approach scope (exit), scope (failure), scope (success) https://tour.dlang.org/tour/en/gems/scope-guards https://tour.dlang.org/tour/en/gems/scope-guards
- josephcsible 5y agoI don't like the nonlocality of this. When I see a closing curly brace, I want to be able to tell what execution will do when it gets there just by looking immediately before the corresponding opening curly brace, but now I'm going to have to look in the entire body of the block too. If I wanted things to be this implicit and nonlocal, then I'd have chosen C++ instead of C.
- laserbeam 5y agoExcept, when you start writing code with defers half your cleanup flags disappear, you end up with fewer branches and it makes it much harder to forget to free temporary allocations. It's really nice to look at only 1 place in code where something was allocated to ensure it also gets deallocated, instead of looking at the curly brace and then thinking: ok, what should be deallocated here, and when should it not be?
- yk 5y agoI don't like it. The great advantage of C is the very simple virtual machine and therefore very easy to reason about code. This bolting on language features is the hallmark of C++, which don't get me wrong has advantages, but in case we want the advantages we can already use C++.
- eptcyka 5y agoThe feature wouldn't introduce much complexity to the abstract machine model IMO, not any more than what cleanup labels already do.
- dgellow 5y agodefer has nothing to do with C++.
- dahfizz 5y agoC++ has try finally, which is the same thing. Instead of bloating C, anyone needing defer can use a subset of C++ that fits their needs.
- Ericson2314 5y agoI dunno this capture stuff is alright but also a bit like blatant appeasement. The point of this feature is to be non-cannonical RAII. I.e. it is about cleaning up a location. This data oriented approach is similar to how "locked data" is more correct and intuitive than "critical sections". Now I get C being low level, so perhaps this is better, but I can't really imagine the thing I am cleaning up (as opposed to auxiliary info like an allocator to deallocate memory with) being captured by value. BTW, speaking of moves, a "set this bool if I use this variable by value" feature would be very useful. Skip C++'s mistake and do Rust's model. Would work great with this feature.
- lelanthran 5y ago> Now I get C being low level, so perhaps this is better, but I can't really imagine the thing I am cleaning up (as opposed to auxiliary info like an allocator to deallocate memory with) being captured by value. I was wondering the same thing; what is the use-case for needing a capture-by-value option at scope exit? Just make all captures a capture-by-reference and it works for all use-cases.
- AndyKelley 5y agoWhat an arrogant use of the word "simple" in the title. * no decision on access to variables * lambdas are involved at all * "appearance in blocks other than function bodies [is] implementation-defined" They cited D in the implementation, but then used Go as the inspiration for the feature. The answer was staring them right there in the face, scope(exit) [1]. Or, you know, they could have cited Zig for the exact syntax they wanted [2]. This feature is completely unusable. Let me demonstrate. You can't use this inside an if statement: if (foo) { const ptr = malloc(...); defer [&]{ free(ptr); } // implementation defined. get fucked } Furthermore, it's go-style, so the defer runs at the end of the function. This is not only implementation defined, it's full-blown undefined behavior because `ptr` goes out of scope before the defer expression runs. Scope-based defer works great. Go-based defer is already problematic enough in Go; in C it's worse than not having defer in the language at all. [1]: https://dlang.org/spec/statement.html#scope-guard-statement https://dlang.org/spec/statement.html#scope-guard-statement [2]: https://ziglang.org/documentation/0.9.0/#defer https://ziglang.org/documentation/0.9.0/#defer
- dnautics 5y agoOh come on now, it's not completely unusable, just "unusable in a ton of important use cases".
- AndyKelley 5y agoPoint taken, but I will defend my claim: The few use cases where it does work are footguns because in the future, you or another collaborator will be tempted to wrap the code into a block, which is normally 100% safe for all other features of the language. It would be easy to do without thinking about it. But if you do it becomes UB as demonstrated above. So the reasonable policy would be to use the same cleanup method everywhere, to avoid footgun firing when code is edited.
- pjmlp 5y agoYep, typical C decision design pattern.
- deleted 5y ago
- karmakaze 5y agoThe Java AutoCloseable seems like a cleaner version of the MS __try/__finally: try (MyCloseable c = useSomething()) { // stuff } Why isn't this the path explored? The main difference would be not having extra nested blocks. That seems relatively minor, no? It can be improved arg list: try (MyCloseable c1 = useSomething(), ElseThing c2 = elseThing()) { // stuff }
- CodesInChaos 5y agoThis syntax relies on destructors, which C doesn't have.
- dandotway 5y agoFeatures that seem like a good idea at the time often don't stand the test of time 20-30 years in the future. In the mid-90s Object-Oriented Programming was super-hyped so a bunch of other languages bolted on OO, such as Fortran and Ada. But now we have Go/Rust/Zig rejecting brittle OO taxonomies because you always end up having a DuckBilledPlatypus that "is a" Mammal and "is a" EggLayer. A great strength of C is that if you want more features you just go to a subset of C++, no need to add them to C. C++ is the big, ambitious, kitchen-sink language. When C++ exists we don't need to bloat C. Fortran was originally carefully designed so that people who aren't compiler experts can generate very fast (and easily parallelized) code working with arrays the intuitive and obvious way. But later Fortran added OO and pointers making it much harder to auto-parallelize and avoid aliasing slowdown. Now that GPUs are rising it turns out that the original Fortran model of everything-is-array-or-scalar works really well for automatically offloading to the GPU. GPUs don't like method-lookup tables, nor do they like lambdas which are equivalent to stateful Objects with a single Apply method. Scientists are moving to CUDA now, which on the GPU side deletes all these features that Fortran was bloated with. Now nVidia offers proprietary CUDA Fortran which is much more in the spirit of original Fortran, deleting OO and pointers for code that runs on GPU. If the ISO standards committee didn't ruin ISO Fortran for scientific computing by bloating it with trendy features we could all be running ISO Fortran automatically on CPUs and GPUs with identical code (or just a few pragmas) and not be locked in to proprietary nVidia CUDA. But GPUs are now mainly used for crypto greed instead of science for finding cancer cures or making more aerodynamic aircraft so maybe it all doesn't matter anyway.
- mst 5y ago> GPUs don't like method-lookup tables, nor do they like lambdas which are equivalent to stateful Objects with a single Apply method. Since I tend towards read-only instance data, I often live my life considering an object to mostly be a bag of closures with a shared outer scope.
- bee_rider 5y agoYeah. I think I'm much less informed on this topic, but my initial thought on reading the "Rationale" section was that this sort of feature would only be helpful in cases where C offered almost no advantages over C++.
- Animats 5y agoThat doesn't belong in C. C doesn't have much in the way of implicit execution. C++ does, and C++ already has destructors.
- AlbertoGP 5y agoI list a few defer-style approaches for C that I know of, including the previous version of this one, in the “Related work” section of my block-based defer for C (presented in HN last August https://news.ycombinator.com/item?id=28166125 https://news.ycombinator.com/item?id=28166125): https://sentido-labs.com/en/library/cedro/202106171400/#deferred-resource-release-related-work https://sentido-labs.com/en/library/cedro/202106171400/#defe...
- abaines 5y agoI feel as though they should change the name of the paper, because this must be one of the most complex takes on 'defer' that I have seen. They make a classic mistake of trying to solve a problem by adding more complexity. They cannot decide on by-value or by-reference, so they take the convoluted C++ syntax to allow specifying one (as well as other semantics?). It does not fit with C in my opinion. It will also hurt third party tools that wish to understand C source code, because they must inherit this complexity. I would prefer a solution where they simply pick one (by ref / by value) and issue a compiler warning if the programmer has misunderstood. For example, pick by-value and issue a warning if a variable used inside the defer is reassigned later in the scope.
- tedunangst 5y agoI think people are getting hung up on the lambda syntax, but it seems they're just taking what they're given. If c23 adds lambdas, at this point I'd say it's more likely to be c++ syntax than c syntax because c++ is already out there. So instead of "to resolve ambiguity we force user to choose" it's more like "the probable lambda syntax already makes this explicit so we will use it too." I think that makes more sense than two different syntaxes for lambda and defer, whatever the other merits of the proposal.
- unwind 5y agoIn all of those examples that talked about which pointer value to capture, it would perhaps have helped to use the built-in (standard, although in my opinion way too few programmers use it) way of indicating that a value won't change, i.e. const: double * const q = malloc(something); /* use whatever magic new syntax here, 'q' will not change in this scope */ That wasn't so hard, in my opinion. They could simply let 'defer' fail if the referenced pointer isn't constant. Also, as a micro-wtf I was confused by the use of 'double', for a generic allocation I would have expected void.
- foxfluff 5y agoSo then you can't for example use defer with a pointer that may get realloc'd in a loop? Or pointer to the root node of tree-to-be-built? What a half-assed defer, in my opinion.
- unwind 5y agoSure, that's a valid point. I confess to being not at all used to thinking of/using 'defer' so I don't have a good grasp of the use cases. Thanks.
- foxfluff 5y agoNo problem. Another point I just thought of is that it is very common to have functions that allocate resources and only free them if an error happens; otherwise said resources are returned to the caller or otherwise retained, and not freed. I don't know how well the proposed defer would work here but as long as you can look at actual variables (and not constants), you could do something like this: int error = 0; void *resource = malloc(N); defer [&]{ if (error) free(resource); } if ((error = do_stuff(resource)) != 0) return NULL; // something went wrong, resource freed if ((error = do_more_stuff()) != 0) return NULL; // something went wrong, resource freed return resource; // all ok, let the caller keep it
- nrclark 5y agoC could really use a standardized defer feature. I don't like the proposed syntax though, because it introduces a new meaning for the & operator. In all other uses, the & operator modifies the type of the operand, to be the opposite of the * operator. In the proposed syntax, & becomes something a little bit different: a C++-style reference. I think it would introduce too much language confusion. A more C-like syntax would be: defer(captures-by-value; captures-by-reference) {}. Defer() statements would be executed at scope-exit, in last-in first-out order. Standard block scoping would apply for anything declared inside of the defer().
- KerrAvon 5y agoThe & syntax builds on a separate proposal for C++-style lambdas in C which would introduce that syntax generally.
- pantalaimon 5y ago+1 We are not using teletypes anymore, the days of confusing single character keywords should truly be over.
- isaiahg 5y ago> Defer() statements would be executed at scope-exit, in last-in first-out order. Standard block scoping would apply for anything declared inside of the defer(). But this still doesn't address the problem the change is used to contain. Should defer free a variable's value as it is when defer is used or the value at the end of the scope. And is it really such huge change. I don't find it too far removed that it becomes confusing and I appreciate the additional control over what should be freed and where. You can imagine a scenario where a variable is being reused multiple times where this kind of additional control might be useful.
- butterisgood 5y agoC is done. It just is. It has had an amazing run and yes we will continue to write code in it - despite all the better alternatives it will probably never die.
- colonwqbang 5y agoWhy are lambdas needed for this? The standard C idiom used for this since forever ("goto out") does not use lambdas.
- daneelsan 5y agoHuh that's a good point.
- mpyne 5y agoThe paper seems to go into this. * There is no generic way to say whether a defer should free up a variable based on the value it has when the defer was encountered, or a potentially-different value the variable might have when the defer is actually run. * So the best thing to do seemed to be to give the programmer the ability to make the choice. * Lambdas are the means they chose to make that happen. But this all seems to stem from a desire to enable a use case beyond what an obvious use of a goto idiom would support.
- camel-cdr 5y agoBecause there are multiple defer proposals and this one is made to be compatible with the lambda proposals. N2589 for example proposes a simpler defer statement that just defers an expression, so e.g. "defer fclose(f);"
- daneelsan 5y agoSome say they don't like it because it it's implicit control flow, i.e. I don't like that my code is being put into the end of the function without it being in the end of the function. I mean OK, but that's what for loops so right? The i++ is put at the end of the "while" loop, together with the exit condition. I think the more important problems are: 1) why be function scoped and not block scoped? 2)why should it be so tied up with lambdas? 1) I don't se why they chose function over scoped so please enlighten me 2) the proposal said there were debates whether defer free(ptr) refers to the ptr where the defer first appears, or to the current ptr that appears when the defer block is being executed. As someone mentioned, gotos already work in the latter way. Same goes with i++ in a for loop, I could do whatever I wanted with the i inside the for, and the i++ or the exit condition would use the latest value of i, not the value at the start of the block.
- camel-cdr 5y ago> I don't se why they chose function over scoped so please enlighten me I don't like it either, but it allows for a simpler syntax, consider e.g. "f = fopen(...); if (f) { defer fclose(); }". You'd need special syntax for the defer to escape the block. Although this example is a bit forced, because usually you'd return from the function on error anyways.
- laserbeam 5y ago> Some say they don't like it because it it's implicit control flow, i.e. I don't like that my code is being put into the end of the function without it being in the end of the function. Wait, what? The whole point of defer is to let you do that. People like defer because it's different control flow. If that's why X doesn't like defer, X shouldn't use it. No big deal.
- daneelsan 5y agoYeah, I do want some kind of defer.
- dataangel 5y agoIt's annoying to see everyone adopting defer (Jai, Go, Zig) and not destructors. Destructors are strictly more powerful. Defer only lets you delay something based on scope, while destructors let you delay based on object lifetime, which might be tied to scope (for an object allocated on the stack) but could also be on the heap. With destructors you can have a smart pointer type where you can't screw up the incrementing and decrementing, with defer you cannot do this.
- typon 5y agoThis is actually a feature, not a bug. With defer the control flow is more obvious and predictable. That’s one of the reasons new languages are adopting it.
- gsliepen 5y agoBut with defer you can easily get the order of things wrong, whereas destructors are automatically called in the correct order.
- watt 5y agoThe advantage of "defer" is that it's explicit. It's right there, and it's crystal clear when it will run. The "implicitness" and hidden nature of destructors is a disadvantage, it's the unpleasant way it can introduce behavior which might be surprising.
- naasking 5y agoDestructors are a lot more complicated for that reason, and the semantics may not always be clear.
- deleted 5y ago[deleted]
- foxfluff 5y agoCertainly if the only thing I want to do is to make sure a piece of code runs when I leave the block or function, defer is the most direct way to do it. I don't care for "strictly more powerful" if it requires something as roundabout as defining an (zero-sized) object with a destructor and allocating an instance of it..
- taeber 5y agoI like the idea of automatic resource cleanup in C and was excited when I read the headline, but I agree with others that the proposed syntax doesn't seem very C-like. I previously shared a "with" statement idea on HN that I thought fit with the rest of the language. with (FILE *fp; fp = fopen(path, "wb"); fclose(fp)) { ... } else { perror("Failed to open file"); } https://github.com/taeber/cwith https://github.com/taeber/cwith https://news.ycombinator.com/item?id=23370693 https://news.ycombinator.com/item?id=23370693 edit: formatting
- deleted 5y ago[deleted]
- okla 5y agoI wonder if this can be emulated with for loop: for(FILE *fp = fopen(path, "wb"); fp != NULL; fclose(fp), fp = NULL) { ... }
- josephcsible 5y agoNo, because exiting the block early (e.g., with return) won't run the cleanup code.
- jstimpfle 5y agoThat's a little bit like saying, it's impossible to write functioning code because you could make a mistake. But an abstraction that can be misused might still be an abstraction worth using. Here is a macro that can be used to emulate a defer #define CONCAT_(a, b) a ## b #define CONCAT(a, b) CONCAT_(a, b) #define UNIQUENAME() CONCAT(i_, __LINE__) #define SCOPE_(counter, init_stmt, exit_stmt) for (int counter = ((init_stmt), 1); counter--; (exit_stmt)) #define SCOPE(init_stmt, exit_stmt) SCOPE_(UNIQUENAME(), (init_stmt), (exit_stmt)) Granted it's a hack, but it can be useful at times. I've used something like it to define a large data hierarchy in code for example, as having to close all the nodes manually is tedious. You could wrap a second for-loop around the definition of the macro to at least be able to catch misplaced "break" statements.
- camgunz 5y agoWhy is this feature entangled with lambdas? Why is defer not a block which is sugar for a goto? Probably the answer is "we want capturing" but this only exists in Go because of its defer scoping weirdness. It also doesn't make the feature foolproof, as in what if I call fclose() outside the defer lambda? Also lambdas and capturing aren't free? What allocates space for them? What is going on with this feature?
- arka2147483647 5y agoGoto may cross variable initialization, so the code in goto may use a variable that does not exist, theoretically. Also a goto cleanup becomes complex if you have, say a dozen variables to cleanup. Anyway, i think this is really syntaxtic sugar for the Cpp destructor. I peresume that the lambda will create an object, which will hook itself to the same mechanism that calls cpp destructors at the end of blocks. And, as all major c compilers are also cpp compilers this seems a good idea to me. And ofcourse the lamda syntax seems same as cpp. So rhats where all this comes from.
- deleted 5y ago[deleted]
- camel-cdr 5y agoBecause there are multiple defer proposals and this one is made to be compatible with the lambda proposals. N2589 for example proposes a simpler defer statement that just defers an expression, so e.g. "defer fclose(f);"
- camgunz 5y agoAhhhhh thank you. I had a minor freak out, thanks for the good info!
- hgs3 5y agoYes! I've been waiting for C to get a defer statement. However, my opinion of this proposal hinges on whether the implementation of lambdas requires runtime support. Not all systems running C have access to the C standard library, much less a runtime to manage lambdas (depending upon how they are implemented). Ideally defer would entirely be handled at compile time.
- laserbeam 5y ago> This indicates that existing mechanism in compilers may have difficulties with a block model. So we only require it to be implemented for function bodies and make it implementation-defined if it is also offered for internal blocks. Can someone explain to me how a feature used for freeing memory, closing file descriptors, releasing locks and all other cleanup scenarios are now possibly entering undefined behavior territory? I get that's not the intent but... Ugh...
- foxfluff 5y agoImplementation defined is quite different from undefined... But I agree, making this bit implementation defined is not cool. I don't have strong opinions on whether defer should be in C or not. It does seem like a potentially useful feature. Whatever they do though, I think they should do it right or not do it at all. The last thing I want is to bloat the language with half-assed features that you can't use half the time because they're not defined well enough. If people want implementation defined features, geez, just keep using compiler extensions until the WG can define a proper standard. Apart from this bit, the proposal seems pretty decent to me. I kinda like the lambda syntax and the control it gives you over the value you capture. Just make it block scoped and don't leave it implementation defined.
- jibalt 5y agoIs it April 1 already? "Whereas gcc’s cleanup attribute is attached to functions, POSIX’ cleanup functions and the try/finally are attached to possibly nested blocks. This indicates that existing mechanism in compilers may have difficulties with a block model. So we only require it to be implemented for function bodies and make it implementation-defined if it is also offered for internal blocks. Nevertheless, we think that it is important that the semantics for the feature are clearly specified for the case of blocks, such that all implementations that do offer such a feature follow the same semantics. Therefore it is also a constraint violation to ask for the feature for blocks on implementations that don’t have support for it." To call this incompetent would be too kind. Completely wrong about the scope of gcc's cleanup attribute, wrong to omit the prior art from Dlang, utterly wrong about what it "indicates", wrong about making it implementation-defined ... and that's just this one section.