The linux kernel has a cool linked list implementation for purposes like this, which IMHO is much prettier. Essentially you put a struct list (or hlist) inside of a struct you use in a list. This struct can be a simple pointer to data, or a full struct itself. Whatever. Then the list macros and functions just work on your data structure.
So, with a clever use of a macro (and assuming you use reference-counted smart pointers or have another way of persisting variables at that time on the stack), you can totally make a fire-and-forget closure in C++. Our solution involved declaring inline classes with some minor stringification magic to guarantee uniqueness.
FTA: The point was not to go into the nuances of closures and lambdas, but only to highlight the basic similarity between function pointers / lambdas / anonymous functions.
This should probably be called "Function pointers in C are AWESOME". because they are. Function Pointers are basically C's implementation for callbacks too! There are some awesome things people can do with fp's, and i especially like C++'s use of function pointers and function objects with respect to the STL algorithms and the use of tr1::bind and std::not1 in the functional header. Cool Stuff!
I don't quite understand what is meant by this. Function pointers are not "underrated"; that implies that it is lower or higher than something. Being the only way to perform an indirect branch in C, it's not really something you rate. It just is.
Through the title, I'm just trying to point out that function pointers are not as well-known in C as in other languages. Programmers should recognize when and how to use them effectively.
A lot of people here have pointed out that closures are not the same as function pointers in C, and for that I thank them. I have added that note to the blog post (it should be up on GitHub shortly), but I would also like to say that the reason for introducing function pointers in that way was to establish a sense of familiarity for those coming from Ruby / Python / JavaScript, who are not well-acquainted with their use in C.
If you know Python / Ruby / Lisp, you might know it by the name ‘lambda’, or if you come from a JavaScript background, you might’ve used it under the name ‘anonymous function’.
This is false. Lambdas capture context; pointed-to functions i C do not.
FTA: The point was not to go into the nuances of closures and lambdas, but only to highlight the basic similarity between function pointers / lambdas / anonymous functions.
Function pointers in C are like salt. Use them sparingly and you can greatly improve the readability and size of your code, use too many of them and you'll end up spoiling the broth and it will become a terrible mess.
[+] [-] sophacles|14 years ago|reply
Implementation details here: http://lxr.linux.no/linux+v3.3/include/linux/rculist.h (also look up list.h).
My favorite part is wrapping your head round the container_of macro central to this. ( http://lxr.linux.no/linux+v3.3/tools/perf/util/include/linux... )
Not that function pointers aren't neat or useful, but sometimes there are other cool solutions too :)
[+] [-] sixbrx|14 years ago|reply
Function pointers can't capture variables from surrounding scopes and so can't depend on runtime values in any easy or safe way.
Just try emulating the following with function pointers:
Sure you could add another input parameter for the C function pointer, but then you couldn't use it anywhere an arity-1 function is required.[+] [-] angersock|14 years ago|reply
[+] [-] jasonlotito|14 years ago|reply
FTA: The point was not to go into the nuances of closures and lambdas, but only to highlight the basic similarity between function pointers / lambdas / anonymous functions.
[+] [-] zerostar07|14 years ago|reply
C++ 's inline templates beat them in performance though.
[+] [-] mangoman|14 years ago|reply
[+] [-] duaneb|14 years ago|reply
[+] [-] matt_yoho|14 years ago|reply
[+] [-] vickychijwani|14 years ago|reply
[+] [-] vickychijwani|14 years ago|reply
[+] [-] wglb|14 years ago|reply
This is false. Lambdas capture context; pointed-to functions i C do not.
[+] [-] jacquesm|14 years ago|reply
http://en.wikipedia.org/wiki/Anonymous_function
vs
http://en.wikipedia.org/wiki/Closure_%28computer_science%29
A lambda function with all its variables bound is a closure iirc.
[+] [-] jasonlotito|14 years ago|reply
[+] [-] mhd|14 years ago|reply
[+] [-] vickychijwani|14 years ago|reply
[+] [-] jacquesm|14 years ago|reply
[+] [-] kbd|14 years ago|reply
[+] [-] smashing|14 years ago|reply