Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casts between pointer-to-function and pointer-to-object in C and C++

Tags:

c++

c

Am i wrong about the following?

C++ standards says that conversion between pointer-to-function and pointer-to-object (and back) is conditionnaly-supported with implementation-defined semantics, while all C standards says that this is illegal in all cases, right?

void foo() {}

int main(void)
{
    void (*fp)() = foo;
    void* ptr = (void*)fp;
    return 0;
}

ISO/IEC 14882:2011

5.2.10 Reinterpret cast [expr.reinterpret.cast]

8 Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cvqualification, shall yield the original pointer value.

I can't find anything about it in C standard right now...

like image 668
FrozenHeart Avatar asked Jan 02 '13 16:01

FrozenHeart


People also ask

What is the difference between function pointer and pointer to a function in C?

As I understand function pointer is a pointer variable that stores address of a function however pointer to a function is a function which takes function pointer as an argument.

Which type of cast allows the conversion of pointers of one type to pointers of another type even if the classes are unrelated?

reinterpret_cast. reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other.

Can you cast function pointer?

Yes, it can. This is purpose of casting function pointers, just like usual pointers. We can cast a function pointer to another function pointer type but cannot call a function using casted pointer if the function pointer is not compatible with the function to be called.

What is the difference between function to pointer and pointer to function explain with example?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.


2 Answers

  • In C++03, such conversions were illegal (not UB). The compiler was supposed to issue a diagnostic. A lot of compilers on Unix systems didn't issue a diagnostic. This was essentially a clash between standards, POSIX vs C++.
  • In C++11, such conversions are "conditionally supported". No diagnostic is required if the system does supports such conversions; there's nothing to diagnose.
  • In C, such conversions officially are undefined behavior, so no diagnostic is required. If the system happens to do the "right" thing, well that's one way to implement UB.
  • In C99, this is once again UB. However, the standard also lists such conversions as one of the "common extensions" to the language:

    J.5.7 Function pointer casts
    A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
    A pointer to a function may be cast to a pointer to an object or to void, allowing a function to be inspected or modified (for example, by a debugger) (6.5.4).

like image 103
David Hammen Avatar answered Oct 17 '22 14:10

David Hammen


You're right, the C(99) standard says nothing about conversion from pointer-to-function to pointer-to-object, therefore it's undefined behaviour.*


*Note, however, that it does define behaviour between pointer-to-function types.
like image 27
Oliver Charlesworth Avatar answered Oct 17 '22 14:10

Oliver Charlesworth