Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't pointers to member functions just memory address like data pointers

Tags:

c++

c

I realized from this FAQ entry that one cannot convert a pointer to member function to/from void*. The reason being pointers to members are not memory addresses exactly like pointers to data! Why so? Please help me get clarified. And this isn't necessarily with member functions but any normal C functions as well, isn't?

like image 599
Prabhu Avatar asked Apr 18 '14 07:04

Prabhu


People also ask

Are pointers just memory addresses?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.

Can we create a pointer to a member function?

You can use pointers to member functions in the same manner as pointers to functions.

Why pointer is not a data structure?

No, a pointer is just a type, not a structure. There are implementations of structures that are types ( std::vector , std::map , ...), but a pointer is not. They are commonly used internally in the implementations of the structures you enumerated, but in itself, a pointer is not a structure.

Are pointers stored in memory?

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware.


2 Answers

pointers to members are not memory addresses exactly like pointers to data! Why so?

Pointers to member functions need to indicate whether the function is virtual, and allow virtual dispatch (perhaps by specifying the index into the vtable, rather than the address of a specific function) if so. This makes them more complicated than just an address.

And this isn't necessarily with member functions but any normal C functions as well, isn't?

Pointers to "normal" (non-member) functions may be converted to object pointers, but not portably. Quoting the standard:

C++11 5.2.10/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, [...]

On many platforms, a (non-member) function pointer is simply a memory address, and the conversion is well-defined. Some platforms have more exotic memory architectures - for example, separate memory spaces for instructions and data - and the conversion may not be allowed on those platforms.

like image 107
Mike Seymour Avatar answered Sep 23 '22 05:09

Mike Seymour


In the C++ standard

As the next FAQ says:

The language does not require functions and data to be in the same address space, so, by way of example and not limitation, on architectures that have them in different address spaces, the two different pointer types will not be comparable.

In §5.2.10/8 (of N3936 specifically) the standard specifies that this is indeed implementation defined:

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 cv- qualification, shall yield the original pointer value.

Here the behavior is well specified.

In the C standard

The C standard doesn't appear to contemplate the conversion from a function pointer to an object pointer. In fact it barely draws a line between them.

It just states, at §6.3.2.3/8, that:

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

At this point the behavior almost seems to be unspecified.

And then later in §6.5.9/6:

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

Here we can see the only trace of an actual difference in:

Two pointers compare equal if and only if both are [..] pointers to the same object (..) or function [..].

The why

As for the "why", it appears to be dependent on the fact that some architectures simply have functions and objects in two address space.

like image 32
Shoe Avatar answered Sep 23 '22 05:09

Shoe