Skip to content

Instantly share code, notes, and snippets.

@mattn
Forked from mooz/closure-test.c
Created March 12, 2010 15:04
Show Gist options
  • Save mattn/330382 to your computer and use it in GitHub Desktop.
Save mattn/330382 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void *create_counter(int n)
{
static int m = 0;
int counter()
{
printf("address in closure %p\n", &m);
return m++;
}
printf("original address %p\n", &n);
m = n;
return counter;
}
int main()
{
int (* func1)(void) = create_counter(5);
int (* func2)(void) = create_counter(10);
/* toooooooooooooooooo bad! */
printf("%d\n", func1());
printf("%d\n", func2());
printf("%d\n", func1());
printf("%d\n", func2());
printf("%d\n", func1());
printf("%d\n", func2());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment