public
Created — forked from jondistad/gist:1196418

inner function closure in C

  • Download Gist
gistfile1.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <stdlib.h>
#include <stdio.h>
 
int(*outer(int val))() {
int inner() {
return val;
}
return inner;
}
 
int main() {
printf("%d\n", outer(5)());
return 0;
}
 
$ gcc -fnested-functions -o closure closure.c
$ ./closure
5

But I can't help thinking inner had been freed before it was called and I got lucky...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.