#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *a = malloc(sizeof(int));
__extension__ void clean(void)
{
free(a);
}
atexit(clean);
return 0;
}
a
is visible inside the nested function, what can cause the segmentation fault?
From the gcc documentation:
If you try to call the nested function through its address after the containing function exits, all hell breaks loose. If you try to call it after a containing scope level exits, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.
atexit
registered functions are called after main
exits.