Function Pointers in C are Underrated
vickychijwani.github.com
vickychijwani.github.com
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 :)
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:
def incrementor(n: Int) = (x:Int) => x + n
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.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 is all called being pedantic. The idea was sound, and, let's be honest, unless you are completely obtuse (and, just to be clear, I don't think you are), I can guess that you understood what he was trying to say. But, you still had to provide a correction.
It's not you. You aren't the only one to do it here. By our very nature, hackers can be very pedantic.
Next time you read an article, and you have the sudden urge to say "Actually..." to correct a single thing, spend a moment to make sure you understand the context. What's the real idea?
Not everyone is going to be an excellent writer. Let's focus on the message, not the poor choice of words. =)
C++ 's inline templates beat them in performance though.
If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM.
To expand on that, make an array of function pointers corresponding to your opcodes. Have the eval() function for a bytecode instruction use the first byte of the bytecode as the array index, and then call the function pointer at that position with the other two bytes as arguments. It's a pretty solid use case.
Also, you're not strictly incorrect--the function pointer does "close over" the execution environment of the system at that time, though that's more a feature of C and globals and pointer arithmetic than anything else. ;)
This is false. Lambdas capture context; pointed-to functions i C do not.
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.
I don't want to sound too harsh, I'm just noting that any blog post that starts out like that is a bit likely to be instantly discarded by some people. And will confuse others later on when they want to learn about closures…
Given that function pointers can't close over variables in the environment, it's a fairly tenuous comparison.
The ability to pass function around is different from the ability of functions to close over their environment. When people talk about closures and their power, they're generally talking about the latter, not the former, at least in my experience.
What you are saying is sort of like saying that Classes and Objects are equivalent, creating a closure creates an instance, wheras a function pointer just references the canonical code.
Also, don't undervalue anonymous local functions, which is another thing that full closures have over C99 functions.