A
Adam Warner
Hello all,
I'm very new to C but I have a number of years of Common Lisp programming
experience. I'm trying to figure out ways of translating higher order
concepts such as closures into C. The code will not be idiomatic C.
GCC has an extension to ISO C that permits nested functions:
<http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html>
For implementing closures they have a serious limitation:
If you try to call the nested function through its address after the
containing function has exited, all hell will break loose. If you try
to call it after a containing scope level has exited, 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.
I'm hopeful that if I heap allocate all closed over variables that I will
simulate closures. I'm aware of the distaste some have for the extension:
<http://groups.google.co.nz/[email protected]>
At this stage my question is elementary: How do I make all hell break
loose in the code below?
#include <stdio.h>
size_t glfn1() {
int x=1;
int inc() {
return printf("%d\n",x=x+1);
}
return (size_t) &inc;
}
int main() {
size_t fn_address=glfn1();
/* All hell will break loose */
fn_address();
return 0;
}
I understand size_t is the ideal type for storing a pointer because it
will also work upon 64-bit platforms that have 32-bit ints.
How do I induce the compiler to accept the address of the function inc as
a function type? The called object of type size_t is not a function type.
Regards,
Adam
I'm very new to C but I have a number of years of Common Lisp programming
experience. I'm trying to figure out ways of translating higher order
concepts such as closures into C. The code will not be idiomatic C.
GCC has an extension to ISO C that permits nested functions:
<http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html>
For implementing closures they have a serious limitation:
If you try to call the nested function through its address after the
containing function has exited, all hell will break loose. If you try
to call it after a containing scope level has exited, 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.
I'm hopeful that if I heap allocate all closed over variables that I will
simulate closures. I'm aware of the distaste some have for the extension:
<http://groups.google.co.nz/[email protected]>
At this stage my question is elementary: How do I make all hell break
loose in the code below?
#include <stdio.h>
size_t glfn1() {
int x=1;
int inc() {
return printf("%d\n",x=x+1);
}
return (size_t) &inc;
}
int main() {
size_t fn_address=glfn1();
/* All hell will break loose */
fn_address();
return 0;
}
I understand size_t is the ideal type for storing a pointer because it
will also work upon 64-bit platforms that have 32-bit ints.
How do I induce the compiler to accept the address of the function inc as
a function type? The called object of type size_t is not a function type.
Regards,
Adam