Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to new-allocate a pointer to function?

Tags:

c++

Pointers to functions are not plain data pointers as they cannot be stored in a void* pointer. Nonetheless, it seems that I can store the copy of a function-pointer in dynamic memory (in gcc and clang) like in the code below. Is such a code legal according to the C++ Standard, or maybe this is some sort of compiler extension?

Moreover, the resulting pointer to function-pointer behaves as a plain data pointer: I can store it in void* and retrieve it from void* by static_cast. Is this behavior guranteed by the Standard?

int main() {   extern void fcn();   void (*fcnPtr)() = &fcn;   void (**ptrToFcnPtr)() = nullptr;    //Make the copy of fcnPtr on the heap:   ptrToFcnPtr = new decltype(fcnPtr)(fcnPtr);   //Call the pointed-to function :    (**ptrToFcnPtr)();    //Save the pointer in void* :   void *ptr = ptrToFcnPtr;   //retrieve the original ptr:    auto myPtr = static_cast< void(**)() > (ptr) ;    //free memory:   delete ptrToFcnPtr ;  } 
like image 597
Adrian Avatar asked Feb 04 '20 12:02

Adrian


People also ask

Can we have a pointer to a function?

You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

Is it possible to create a pointer to member function of any class?

Pointers to members allow you to refer to nonstatic members of class objects. You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.

Do function pointers need to be freed?

No. You must not because free(ptr) is used only when pointer ptr is previously returned by any of malloc family functions. Passing free a pointer to any other object (like a variable or array element) causes undefined behaviour.


2 Answers

While function pointers are not object pointers, "pointer to function of some type" is still an object type [basic.types]/8. Thus, function pointers are themselves objects, just the thing they point to is not.

Thus, you sure can create an object of function pointer type via a new expression…

like image 161
Michael Kenzel Avatar answered Sep 20 '22 01:09

Michael Kenzel


as they (function pointers) cannot be stored in a void* pointer.

Actually, storing a function pointer as a void* is conditionally supported. This means that either it can or cannot be stored depending on the language implementation. If the language implementation supports dynamic loading, then converting function pointer in void* probably is supported. GCC, Clang and MSVC all support this:

reinterpret_cast<void*>(&function); 

Is it legal to new-allocate a pointer to function?

Sure. All pointers, including function pointers, are objects and all objects can be allocated dynamically.

Moreover, the resulting pointer to function-pointer behaves as a plain data pointer

Function pointer is an object. Pointer to a function pointer not only "behaves as", but is a pointer to an object.

I can store it in void* and retrieve it from void* by static_cast. Is this behavior guranteed by the Standard?

Conversion between pointer to void and pointer to object is allowed, yes. And round-trip conversion is guaranteed to yield the original pointer.

like image 37
eerorika Avatar answered Sep 20 '22 01:09

eerorika