3

I was trying to understand the difference between closures and function pointers, and I came across this answer in SO

What I don't understand is this code

BOOL (*lessThanTest)(int);
int lessThan = 100;

lessThanTest = &LessThan;

BOOL LessThan(int i) {
   return i < lessThan; // compile error - lessThan is not in scope
}

Why there is a compile error consideringn that lessThan is a global variable, it can be accessed from within LessThan function, did I miss something?

EDIT

This is not my code, it's taken from an answer in SO Function pointers, Closures, and Lambda

Community
  • 1
  • 1
Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • can you post minimum compilable code? (comment out the part that gives you the error) – Luchian Grigore Nov 11 '11 at 15:11
  • That wouldn't even compile, since `LessThan` isn't yet declared before `lessThanTest = &LessThan;` –  Nov 11 '11 at 15:11
  • This is invalid code in C. You can achieve same result as in c# using c++ and functors. – joy Nov 11 '11 at 15:20
  • 1
    @neagoegab -- it's not supposed to be valid C and it's not his example. Read the linked question for more context. – Matt Fenwick Nov 11 '11 at 15:21
  • But it is still a bad example with incomplete specifications... Closure can have state same as a functor while a function pointer does not have a state and the state is maintained in a global variable or passed as an argument.... – joy Nov 11 '11 at 15:34

4 Answers4

2

Closures take all the variables in their lexical scope along for the ride, possibly extending their lifetimes. Function pointers don't -- if the variables referenced inside their code disappear, they're hosed.

The code example you've given is a little bit confusing. I believe that it's meant to be inside of a function, meaning that lessThan is a local variable. If that scope is exited, but the function pointer still exists, then its code would have a reference to a non-existent variable -- lessThan.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
1

You missed a paragraph in that answer:

But, now I have to pass the 2 arguments when I evaluate it. If I wished to pass this function pointer to another function where lessThan was not in scope, I would either have to manually keep it alive by passing it to each function in the chain, or by promoting it to a global.

In what you posted, int lessThan is not meant at global scope, it should be assumed to be in a function somewhere.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • I think that paragraph is talking about the second code snippet – Mansuro Nov 11 '11 at 15:15
  • (I think) It talks about both. Neither are strictly valid `C` at global scope anyway. – Mat Nov 11 '11 at 15:18
  • "I could define a function pointer " ... "If I wished to pass **this** function pointer " – Mansuro Nov 11 '11 at 15:23
  • I don't see your point. If `lessThan` was at global scope, there would be no compiler error (assuming the rest of the code was correct). Both examples manipulate a function pointer, the second one has different object lifetime/visibility issues than the first, but both have the same fundamental problem - C doesn't have closures (or lambdas, or nested functions for that matter), you need to implement workarounds yourself. – Mat Nov 11 '11 at 15:32
  • my point is, what can be done in closures can be done using functions pointers with some workarounds like you said – Mansuro Nov 11 '11 at 15:34
  • Then what is your question? The code you quoted isn't valid at global scope, and you understand stuff needs to be done to simulate closures. – Mat Nov 11 '11 at 15:39
  • My question is about that snippet of code, for what reason did the author say it's a compile error, while it can work if lessThan variable is a global variable – Mansuro Nov 11 '11 at 15:43
  • It's a compiler error if `lessThan` is not in scope in the `LessThan` function. Since C doesn't have nested functions (or classes), the only ways for `lessThan` to be in scope are 1. global, 2. argument. – Mat Nov 11 '11 at 15:46
0

Well, no, if lessThan is global, it shouldn't produce compile error, though you can hardly tell by this fragment what is meant to be where. lessThanTest=&LessThan; is definitely from some local scope, for instance.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

This code has some problems:

  • You declare lessThanTest as an uninitialized function pointer
  • You can't assign something to it later under global scope
  • You're using LessThan before declaring it.
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
sidyll
  • 57,726
  • 14
  • 108
  • 151