Skip to content

Instantly share code, notes, and snippets.

@mattyoho
Forked from catharinejm/gist:1196418
Created September 7, 2011 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyoho/1200480 to your computer and use it in GitHub Desktop.
Save mattyoho/1200480 to your computer and use it in GitHub Desktop.
inner function closure in C
#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
@mattyoho
Copy link
Author

mattyoho commented Sep 7, 2011

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