A simple, stb-style single-header library which adds a compile-time defer block and autofree attribute to C/C++.
{
autofree int* i = malloc(sizeof(int));
printf("%d\n", i);
// i is freed here
}
{
struct MyStruct* s = MyStruct_create();
defer({
MyStruct_destroy(s);
});
printf("%p\d", s);
// s is freed here using MyStruct_destroy
}
All info can be found in the header file, but to summarize:
Do this:
#define DEFER_IMPL
before you include this file in one C or C++ file to create the implementation.
i.e. it should look like this:
#include ...
#include ...
#include ...
#define DEFER_IMPL
#include "defer.h"
Licesed under MIT license, do what you want with it.