Live data from Hacker News

Function Pointers in C are Underrated

vickychijwani.github.com

21–30 of 33 posts

Re: Function Pointers in C are Underrated

#21
post #10

The author claims you might know it as a lambda or anonymous function in other languages, but I think function pointers come up short in comparison. 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…

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.

Re: Function Pointers in C are Underrated

#22
post #14

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.

I believe it is closures that capture context, lambdas are just anonymous functions, see:

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.

Re: Function Pointers in C are Underrated

#23
post #12

Earlier quoted context omitted.

Not as well known to C programmers, or not as well known to programmers who don't program C? I don't know a single C programmer that doesn't regularly use function pointers.

Being a student who's been in the process of learning C over the past 3 years, I for one have not come across many examples of function pointers in actual use, apart from the standard library functions qsort and bsearch.

Interesting. Function pointers are powerful, but are hardly a replacement for closures.

Re: Function Pointers in C are Underrated

#24
post #2

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.

I interpret the statement as being closer to "function pointers are under-discussed".

Indeed. While someone might talk about lambda functions very early into a discussion of Ruby, Python, etc., pointers-to-functions in C are almost never discussed in basic Computer Science.

Re: Function Pointers in C are Underrated

#25
post #12

Earlier quoted context omitted.

Not as well known to C programmers, or not as well known to programmers who don't program C? I don't know a single C programmer that doesn't regularly use function pointers.

Being a student who's been in the process of learning C over the past 3 years, I for one have not come across many examples of function pointers in actual use, apart from the standard library functions qsort and bsearch.

No harm. :)

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. ;)

Re: Function Pointers in C are Underrated

#27
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.

Re: Function Pointers in C are Underrated

#28

Earlier quoted context omitted.

Being a student who's been in the process of learning C over the past 3 years, I for one have not come across many examples of function pointers in actual use, apart from the standard library functions qsort and bsearch.

No harm. :) 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…

I'm not sure that closing over only global really counts...

Re: Function Pointers in C are Underrated

#29
post #28

Earlier quoted context omitted.

No harm. :) 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…

I'm not sure that closing over only global really counts...

Yes, I suppose it doesn't, as you can't really freeze the execution state (or the relevant parts of it) for the use of the closure--e.g., somebody else can change the memory values before your code gets run and so you act on stale data. :(
Post reply on HN