News for hackers. Stuff that matters.
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.
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.
Re:A Defer Mechanism for C by saagarjha on Monday December 14, 2020 @07:34PM (#25425467)
The defer would just run at the end of each iteration.
Re:A Defer Mechanism for C by skohan on Monday December 14, 2020 @05:43PM (#25424366)
Is it actually a lambda, or is it just essentially appending statements to the end of the scope?
Re:A Defer Mechanism for C by anfilt on Monday December 14, 2020 @08:36PM (#25425984)
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.