3 ms·
In 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 funct
by cpuguy83 5y ago
In 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.