Created — forked from jondistad/gist:1196418

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

inner function closure in C

View gist:1200480
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
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.