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 ; }
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.
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.
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.
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…
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With