4 ms·
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
by apankrat 6y ago
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.
- IAmADHDToo 6y agoThat'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.
- saagarjha 6y agoThe defer would just run at the end of each iteration.
- deleted 6y ago[deleted]
- skohan 6y agoIs it actually a lambda, or is it just essentially appending statements to the end of the scope?
- anfilt 6y agothat 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.