https://github.com/jomy10/defer
A simple, single-header library which implements defer and autofree at compile-time in C/C++
https://github.com/jomy10/defer
autofree c cpp defer single-header single-header-lib single-header-library
Last synced: about 1 year ago
JSON representation
A simple, single-header library which implements defer and autofree at compile-time in C/C++
- Host: GitHub
- URL: https://github.com/jomy10/defer
- Owner: Jomy10
- License: mit
- Created: 2023-11-08T13:47:29.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-20T12:22:15.000Z (over 1 year ago)
- Last Synced: 2025-03-17T11:52:01.273Z (about 1 year ago)
- Topics: autofree, c, cpp, defer, single-header, single-header-lib, single-header-library
- Language: C
- Homepage:
- Size: 8.79 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# defer.h
A simple, stb-style single-header library which adds a compile-time defer block and autofree attribute to C/C++.
## Autofree
```c
{
autofree int* i = malloc(sizeof(int));
printf("%d\n", i);
// i is freed here
}
```
## Defer
```c
{
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:
```c
#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:
```c
#include ...
#include ...
#include ...
#define DEFER_IMPL
#include "defer.h"
```
## License
Licesed under MIT license, do what you want with it.