Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

defer.h

A simple, stb-style single-header library which adds a compile-time defer block and autofree attribute to C/C++.

Autofree

{
autofreeint*i=malloc(sizeof(int));
printf("%d\n", i);
// i is freed here
}

Defer

{
structMyStruct*s=MyStruct_create();
defer({
MyStruct_destroy(s);
});
printf("%p\d", s);
// s is freed here using MyStruct_destroy
}

Including the header

All info can be found in the header file, but to summarize:

Do this:

#defineDEFER_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 ...
#defineDEFER_IMPL#include"defer.h"

License

Licesed under MIT license, do what you want with it.

About

A simple, single-header library which implements defer and autofree at compile-time in C/C++

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Contributors

Languages