Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reinterpret_cast to void* not working with function pointers

Tags:

c++

casting

arm

I want to reinterpret cast a function pointer into a void* variable. The type of the function pointer will be of type Class* (*)(void*).

Below is the sample code,

class Test
{
    int a;
};

int main()
{
    Test* *p(void **a);
    void *f=reinterpret_cast<void*>(p);     
}

The above code works well with Visual Studio/x86 compilers. But with ARM compiler, it gives compilation error. Don't know why.

Error: #694: reinterpret_cast cannot cast away const or other type qualifiers

I read the explanation in Casting a function pointer to another type

I was concerned about the below explanation.

Casting between function pointers and regular pointers (e.g. casting a void (*)(void) to a void*). Function pointers aren't necessarily the same size as regular pointers, since on some architectures they might contain extra contextual information. This will probably work ok on x86, but remember that it's undefined behavior.

How to do such conversions from void (*)(void*) -> void* effectively so that atleast it compiles almost the same in most of the compilers ?

like image 987
vprajan Avatar asked Aug 20 '09 08:08

vprajan


4 Answers

reinterpret_cast can't be used to cast a pointer to function to a void*. While there are a few additional things that a C cast can do which aren't allowed by combination of static, reinterpret and const casts, that conversion is not one of them.

In C the cast is allowed, but it's behavior isn't defined (i.e. even round trip isn't guaranteed to work).

Some POSIX functions need the conversion to be well useful.

I've played with several compilers I've here:

  • none prevent the C cast, even in the highest conformance mode. Some give a warning depending on the warning and conformance level, others gave no warning whatever I tried.
  • the reinterpret_cast was a error with some compilers even in the more relaxed level while other accepted it in all case without ever giving a warning.

In the last available draft for C++0X, the reinterpret_cast between function pointers and objects pointers is conditionally supported.

Note that if that make sense or not will depend on the target more than the compiler: a portable compiler like gcc will have a behavior imposed by the target architecture and possibly ABI.

As other have make the remark,

Test* *p(void **a);

defines a function, not a pointer to function. But the function to pointer to function implicit conversion is made for the argument to reinterpret_cast, so what reinterpret_cast get is a Test** (*p)(void** a).

Thanks to Richard which makes me revisit the issue more in depth (for the record, I was mistaken in thinking that the pointer to function to pointer to object was one case where the C cast allowed something not authorized by C++ casts combinations).

like image 178
AProgrammer Avatar answered Oct 04 '22 23:10

AProgrammer


reinterpret_cast can only be used to

  • add constness
  • convert a pointer to an integral type large enough to hold it and back
  • convert a pointer to a function to a pointer to a function of different type
  • convert a pointer to an object to a pointer to an object of different type
  • convert a pointer to a member function to a pointer to a member function of different type
  • convert a pointer to a member object to a pointer to a member object of different type
  • and reinterpret_cast<T&>(x) is equivalent to *reinterpret_cast<T*>(&x) (using builtin & and *) whenever the second cast is possible using the above rules.

(See Section 5.2.10 of the standard)

This means in particular that a cast from a pointer to function to void * is not possible, but you can cast it to void(*)().


EDIT (2017): The answer above is only correct for C++03. In C++11 through C++17, it is implementation defined if conversions between function pointers and void * are allowed. This is usually the case on POSIX compatible systems because dlsym() is declared to return void *, and clients are expected to reinterpret_cast it to the correct function pointer type.

See cppreference.com for the full list of conversions allowed.

like image 39
Tobias Avatar answered Oct 04 '22 22:10

Tobias


If you're just looking to store different types of function pointer in a list then you can cast to a common function pointer type:

class Test {
  int a;
};

int main()
{
  Test* *p(void **a);
  void (*f)()=reinterpret_cast<void (*)()>(p);
}

This is valid to do via reinterpret_cast (5.2.10/6):

A pointer to a function can be explicitly converted to a pointer to a function of a different type. The effect of calling a function through a pointer to a function type (8.3.5) that is not the same as the type used in the definition of the function is undefined. Except that converting an rvalue of type "pointer to T1" to the type "pointer to T2" (where T1 and T2 are function types) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified.

like image 45
Richard Corden Avatar answered Oct 04 '22 23:10

Richard Corden


Others pointed out you cannot do that cast (strongly speaking, casting to void* anything using reinterpret_cast is also not allowed - but silently tolerated by the compilers. static_cast is intended to be used here).

I usually do the following, which is doing a type pun, and is the recommended way to do it, according to the manpage of dlopen (which is about doing the converse - casting from void* to a function pointer). Taking the address of the function pointer will give you a data-pointer: Pointer to a function pointer. This will allow you to cast it to void*. It pretends it's pointing to a void* (instead of a function pointer), and then reads it.

Test* (*pF)(void **a);
void *p = *(void**)(void*)&pF;

The intermediary cast to void* makes it equivalent to using two static_cast's internally, and makes GCC be quiet about warning about a type pun. Using C++ Style casts, this looks like a combination of two static_cast's

void *p = *static_cast<void**>(static_cast<void*>(&pF));

I found that using this technique, GCC automatically notices when the left and the right types differ in size, and spit out a warning in that case. Needless to say that like with all techniques that try to work around this limitation, this is undefined behavior.


If you have a function, and want a void* pointing to it, you can do it all in one line, but that's a bit cumbersome on the syntax. Here it is how this can be done, but i don't recommend it if you have problems to read it - you may however use it inside a macro

// using the function declaration you provided
Test** pF(void **a); 
void *p = *(void**)(void *) &(Test** (* const&)(void **a))&pF;

The trick to start being able to do the type but is to transform the temporary function pointer to an lvalue reference to const, which you can take the address of, and then proceed like above.

Using explicit C++ style static_cast casts, this looks much more complicated, because you have to take the constness into account. The C style cast automatically dealed with that. Have fun!

int main() {
    Test** pF(void **a);
    void *p = *static_cast<void* const*>(
      static_cast<void const*>(
        &static_cast<Test** (* const&)(void **a)>(&pF)));
}
like image 43
Johannes Schaub - litb Avatar answered Oct 04 '22 23:10

Johannes Schaub - litb