Skip to content

Instantly share code, notes, and snippets.

@jirfag
Created April 15, 2015 14:44
Show Gist options
  • Save jirfag/c9f65e6b2c31ea94d3b7 to your computer and use it in GitHub Desktop.
Save jirfag/c9f65e6b2c31ea94d3b7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define UNIQUE_FUNC_NAME TOKENPASTE2(cleanuper_func_, __LINE__)
#define UNIQUE_VAR_NAME TOKENPASTE2(cleanuper_var_, __LINE__)
#define SCOPE_EXIT(code_) \
void UNIQUE_FUNC_NAME(int *t __attribute__((unused))) \
code_ \
int UNIQUE_VAR_NAME __attribute__((cleanup(UNIQUE_FUNC_NAME)));
int main()
{
SCOPE_EXIT({ printf("i'm in scoped exit 1\n"); });
SCOPE_EXIT({ printf("i'm in scoped exit 2\n"); });
printf("i'm before return\n");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment