Hacker News new | ask | show | jobs
by seodisparate 526 days ago
One can use C's `cleanup` attribute like a defer. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute...

The function signature is `void(MyType*)` if it is used like:

    __attribute__((cleanup(MyTypeCleanupFunction)))
    MyType myType;
If it's a pointer, it'll call the cleanup as a double ptr, and etc. Note that the specified cleanup function can do anything you want to, though the intended purpose is to cleanup the variable when it goes out of scope.
1 comments

That isn't a C feature, it's a feature of GCC and other compilers. While I have used (and horribly overused) `cleanup` in personal projects, it's not a replacement for a feature being in the standard.