Skip to content

Instantly share code, notes, and snippets.

@petabyt
Last active December 18, 2021 20:16
Show Gist options
  • Save petabyt/fa20301f0f343a25c075530456a8dca7 to your computer and use it in GitHub Desktop.
Save petabyt/fa20301f0f343a25c075530456a8dca7 to your computer and use it in GitHub Desktop.
autofree.h: Hijack malloc and free on exit(), or autofree()
void *allocList_[1000];int allocn_=0;void *malloc_(long unsigned int n){
void *x=malloc(n);allocList_[allocn_]=x;allocn_++;return x;}void autofree(){
while (allocn_){allocn_--;free(allocList_[allocn_]);}}
#define malloc(x) malloc_(x)
#define exit(x) autofree(x);exit(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment