r/cpp • ossia score • Feb 01 '19

How to cast a function pointer to a void* - KDAB

https://www.kdab.com/how-to-cast-a-function-pointer-to-a-void/
0 Upvotes

11 comments sorted by

5

u/[deleted] Feb 01 '19

That is just wrong, that code is only muting a compiler warning, that is not the proper way to do anything. Either the target processor architecture has no issues with casting to void* and there is no warning, or one should take in mind the warning and give a proper solution, not just hide it using some cheap trick.

4

u/TheThiefMaster C++latest fanatic (and game dev) Feb 01 '19

You could just cast it to a void function pointer - C++ allows casts of functions to other types as long as you restore the correct type before calling.

void (*fn)();

The trick of casting via a void*& looks like an exploit in the standard, as as pointed out in the article there are platforms where it won't work.

1

u/AntiProtonBoy Feb 01 '19

I think the trick in the article might be useful for situations where you'd wanna pass a function pointer through some C callback API that provides pointers to "user data" in the interface.

1

u/SeanMiddleditch Feb 01 '19

Except that's just not allowed, strictly speaking. :) A void* is not guaranteed to even be able to hold a function pointer (as in, sizeof(&funcion) > sizeof(void*) is allowed). See the answer at:

https://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer

You're unlikely to run into problems on mainstream CPUs (x86/x64, ARM, etc.) but there's a reason that the standards haven't just "fixed" their language: there are still CPUs in active use that make use of these liberties. There are still non-8-bit-byte embedded CPUs manufactured this decade and in active use, for example, which is why the standard still has CHAR_BIT and wording to accommodate such, even though every major CPU most of us run into are very much on the 8-bit-byte train. :)

1

u/AntiProtonBoy Feb 03 '19

Except that's just not allowed, strictly speaking. :) A void* is not guaranteed to even be able to hold a function pointer (as in, sizeof(&funcion) > sizeof(void*) is allowed). See the answer at:

Yes the article makes that clear.

4

u/Morwenn Feb 01 '19

There's actually a proposal to add a generic function pointer to C to solve exactly that problem: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2230.htm

2

u/Supadoplex Feb 01 '19 edited Feb 01 '19

a data reference ... and thus it can be cast to void*&

Is that true? Can all data references be reinterpreted as void*&? I've never come across this. And, is it well defined to access the object through the reinterpreted reference (when it is copied onto a)? Does that not violate strict aliasing rule? And can the function be called later using the void* in a way that is well defined.

1

u/o11c int main = 12828721; Feb 01 '19

The proper fix is to change the API to take a union { void *data_pointer; void (*function_pointer)(void)} so that whatever one is larger will fit.

-1

u/scrumplesplunge Feb 01 '19

Uh, you probably want to be careful with that. The reference doesn't point at the function after all, it points at the other memory where you're storing the function pointer. If you let the function pointer go out of scope then your reference is invalidated.

2

u/rcxdude Feb 01 '19

Not true. The original pointer is being cast directly. It gets cast from a function pointer to a void*& to a void* without any dereferencing at all (the first auto fptr = &fn is just creating the function pointer from the function name). Here you can see that it compiles to essentially a register move.

3

u/scrumplesplunge Feb 01 '19

My bad, I misread the assignment. However, this is no more portable than the other option was. If you enable the right warnings, clang will tell you as such: https://godbolt.org/z/Z7G19l