我从这个中文博客http://chenyufei.info/blog/2011-02-28/wrap-c-function-closure-gcc-nested-function/得到这个问题 作者想在c语言中使用闭包,他发现GCC具有嵌套函数(和闭包)的能力。 例如:
typedef int (*func_t)(int arg);
func_t create_wrap_function(func_t f) {
int wrapped(int arg) {
// call original function
int val = f(arg);
fprintf(log_func_call, "arg: %d ret: %d", arg, val);
return val;
}
return wrapped;
}
至少根据 GCC 文档,您不应该这样做:
“如果你试图在包含函数退出后通过它的地址调用嵌套函数,所有的地狱都会崩溃。”
http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Nested-Functions.html