Re:A Defer Mechanism for C
by apankrat on Monday December 14, 2020 @10:03AM
(#25418325)
Every defer statement is ultimately pushing a lambda into some stack that is executed when the defer block is exited. With an exception of setjmp/longjmp, I can't think of an existing C construct with similar run-time complexity.
[ Parent
| on HN ]
Re:A Defer Mechanism for C
by IAmADHDToo on Monday December 14, 2020 @02:54PM
(#25422363)
That's indeed tricky: if there's a for loop inside the guarded block which generates many defers, a stack would have to be created to handle them somehow.
The defer would just run at the end of each iteration.
Is it actually a lambda, or is it just essentially appending statements to the end of the scope?
that depends for instance you can safely call free on NULL so you could just append.
However, for some things you can only free/return resources if you successfully created that resource. At which point you would need to use something like a stack.