Defer

Run Settings
LanguageC++
Language Version
Run Command
#include <stdio.h> #include <stdlib.h> #include <assert.h> struct DeferConversionStruct{ void **ptr; DeferConversionStruct(void **in) { ptr = in; } #define CreatePtrConversion(type) \ DeferConversionStruct(type **in) \ { ptr = (void **)in; } CreatePtrConversion(int) CreatePtrConversion(float) CreatePtrConversion(double) }; struct DeferStruct{ void (*function_ptr)(void *); void **data_ptr; ~DeferStruct() { assert(*data_ptr); function_ptr(*data_ptr); } }; #define CONCAT_(a,b) a##b #define CONCAT(a,b) CONCAT_(a,b) #define DEFER(f_ptr_in, d_ptr_in) \ DeferStruct CONCAT(SomeRandomVariable,__COUNTER__) = { \ .function_ptr = (f_ptr_in), \ .data_ptr = DeferConversionStruct{d_ptr_in}.ptr \ } int main() { int *a = 0; DEFER(&free, &a); a = (int *)malloc(31); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines