#include #include #include "defer.h" struct Test { inline Test() { puts("Constructor of test"); } inline ~Test() { puts("Destructor of test"); } }; int main() { int n; printf("Allocate this much bytes: "); scanf("%d", &n); void *ram = malloc(n); defer { free(ram); printf("%d bytes freed\n", n); }; unsigned char cs = 0; unsigned char *bytes = (unsigned char*) ram; for(int i = 0; i < n; ++i) { cs += bytes[i]; } printf("Checksum of found bytes: %d\n", cs); Test t; // exit(0); // This does not release resources! But neither calls destructors properly anyways. return 0; }