Skip to content
/ defer Public

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

License

Notifications You must be signed in to change notification settings

Jomy10/defer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

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

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

Defer

{
    struct MyStruct* 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:

#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"

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

License

Stars

Watchers

Forks

Languages