Is it possible/safe/sane to pass around a function pointer to a static function?

It is completely safe to do so, and often useful. The same goes for static variables and data pointers. If the compiler wants to do any fancy optimizations (like nonstandard calling conventions, inlining, refactoring, etc.) that could interfere with the ability to call the function through a function pointer from a different translation unit, it's the compiler's responsibility to either determine that the address of the function is never taken, or to generate multiple versions of the code, one of which is safe to be called from outside with the standard calling convention.

Up vote 3 down vote favorite share g+ share fb share tw.

Let’s say I only want to expose a function from one of my files by passing out a function pointer to that function. Is it safe to declare that function as static? Are compilers allowed to do any judo that would invalidate my function pointer, or make it meaningless outside the context of that file, since the function is declared as specific to that file?

Not my code, but a (silly) example of what I mean: void static cool_function(void); void extern (*cool_function_ptr)(void); // Actually, I’m not sure of where the `extern` goes in a function- // pointer declaration. Damn you, confusing function-pointer syntax! Given that code (or the syntactically correct approximation thereof), would it be illegal to access cool_function_ptr from another file?

C static function-pointers extern linkage link|improve this question asked Feb 28 '11 at 4:54elliottcable8402823 94% accept rate.

1 All very good answers. Sometimes Stack Overflow surpasses my expectations! :D – elliottcable Feb 28 '11 at 5:04 Only sometimes?

=) – Conrad Meyer Feb 28 '11 at 5:10 You can make that always. You will be amazed on different dimensions of answers that comes out when you ask it on SO. – Shamim Hafiz Feb 28 '11 at 5:23 I usually get quite a lot of irrelevancy, as well as not-well-thought-out answers that somebody sniped in just to be one of the first responses on a new question, and thus be almost guaranteed an upvote (or even accept.

) It’s rare that every single answer is relevant, much less every single answer being useful! – elliottcable Feb 28 '11 at 5:42.

This is exactly what I wanted to know. Any chance you have a few relevant links to the spec? I trust you word on it, though.

Just curious, always want to learn more. – elliottcable Feb 28 '11 at 5:03 The relevant part of the spec is simply the text about function pointers, and the lack of any statement forbidding using pointers to functions with static linkage. – R.. Feb 28 '11 at 5:07 3 This is used all the time, such as when filling in structs of function pointers representing a common API implemented by multiple modules (e.g. : filesystems in a kernel).

– Conrad Meyer Feb 28 '11 at 5:09.

Sure. A function pointer is just an address, there's nothing magical about it. Here's an example: $ cat Makefile .

PHONY: all all: main . /main main: main. C other.

C gcc -o main main. C other. C $ cat main.

C #include static void goodnight(){ printf("Goonight, gracie! \n"); return; } int main(char * argv, int argc){ other(goodnight); return 0; } $ cat other. C #include void other(void(*fp)()){ fp(); return ; } $ make gcc -o main main.

C other. C . /main Goonight, gracie!

$ The hard part is getting the declaration of a function taking a pointer to function returning void right.

See my discussion with Aredridel below; I’m not worried about getting an address, I’m worried about whether an optimizing compiler can trash said address. Make sense? – elliottcable Feb 28 '11 at 5:02 Yes, and the answer is "no".

In fact, it might very well thwart other optimizations, since it can't (for example) identify inline-able code, since the compiler doesn't know which code will be there. – Charlie Martin Feb 28 '11 at 5:24.

There's nothing different about a pointer to a static function. However, passing the pointer around may be slightly less efficient than allowing it to be called directly. It wasn't entirely clear to me why you're doing that, but it didn't make much sense to me.

Definitely allowed, potentially perfectly reasonable The language allows you to pass a pointer to a static function outside of a module. Therefore, any tricks that the compiler might do in compiling intra-module calls must somehow be compatible with what you are doing. In general, I would say it's actually a good code pattern, relative to making the function global, because you are additionally encapsulating the function, although one could make the opposite argument with respect to a simple static function with no external references.

All-in-all, I think it's the design of your objects and your application that matters, and not whether you globally reference a static function.

The only thing that might bite you is if the function is inlined -- and taking the address of it for a pointer should keep that from happening. Doing this kinda keeps the caller from knowing the full signature, so you're playing with fire and hoping things line up though. And it's "extern void" and "static void" -- storage class, then return type.

That’s what I’m worried about. Not whether the compiler will allow it, but whether it’s legal for optimizations to subsequently trash my pointer. Got relevant links to the specification, by any chance?

(Also, aren’t the storage class and type interchangeable? ) – elliottcable Feb 28 '11 at 5:00 Also, what do you mean, by “keeps the caller from knowing the full signature? ” The signature would still be in the header; the API consumer would simply be forced to call the function through the pointer instead of directly, yes?

– elliottcable Feb 28 '11 at 5:01 That would depend on how the caller gets the pointer. If you're declaring it static in the header, then the caller gets its own copy and the compiler carps about not having it implemented in that unit. The signature then matches but at the cost of having confused things.

You could typedef it though, and make it a pointer of that type. – Aredridel Feb 28 '11 at 5:15 And optimizations should never trash valid code like that. Taking the address of a function is legal unless you specifically say "this function is really special and implspecific inline foo".

– Aredridel Feb 28 '11 at 5:16 Thanks for the info. And I’m wondering who voted you down, because you’re back to 0 on this post and 19 overall. O_O – elliottcable Feb 28 '11 at 5:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions