views:

154

answers:

5

I am trying to print the address of a virtual member function. If I only wants to print the address of the function I can write:

print("address: %p", &A::func);

But I want to do something like this:

A *b = new B();

printf("address: %p", &b->func); 
printf("address: %p", &b->A::func);

however this does not compile, is it possible to do something like this even do looking up the address in the vtable is done in runtime?

+1  A: 

First, you could be a bit more specific on what your compiler fails. Then you should have a look into pointer to member syntax, this is a bit tricky. Googling for that immediately found me that: http://www.goingware.com/tips/member-pointers.html

Also this is definitively not a C question.

Jens Gustedt
+1 for the great link
sinec
That doesn't answer the qestion, its about how to get the adress of the virtual function that will be called for some arbitrary instance.
Georg Fritzsche
Jens Gustedt
Thinking of it, already your idea to print the address of a function at all is not well defined. With "%p" you can only print pointers that are compatible with void*. Function pointers aren't, not every pointer is `just an address'. Any decent compiler should already give you at least a warning on your first printf line.
Jens Gustedt
+1 for the link.
Patrick
+2  A: 

Doesn't make a lot a of sense to me. If you have a normal function:

void f( int n ) {
}

then you can take its address:

f

but you cannot take the address of a function call, which is what you seem to want to do.

anon
@GMan That's what I thought I said. Anyhow, I don't think this is possible.
anon
A: 

From what I can tell in the standard, the only time you get dynamic binding is during a virtual function call. And once you've called a function, you're executing the statements within the function (i.e., you can't "stop halfway" into the call and get the address.)

I think it's impossible.

GMan
I suppose it's impossible portably, but knowing the implementation it should be possible to inspect the virtual table of an object at runtime.
Matthieu M.
+1  A: 

Pointers to member functions are not always simple memory addresses. See the table in this article showing the sizes of member function pointers on different compilers - some go up to 20 bytes.

As the article outlines a member function pointer is actually a blob of implementation-defined data to help resolve a call through the pointer. You can store and call them OK, but if you want to print them, what do you print? Best to treat it as a sequence of bytes and get its length via sizeof.

AshleysBrain
Yet the problem remains: how do you identify the function that get's called through a `virtual` call :) ?
Matthieu M.
That wasn't part of the question was it? But I'd answer 'by calling it' :)
AshleysBrain
A: 

Will this code print the right address? It will print the address of the function in the vtable, but is it correct:)?

struct A
{
    virtual void func() { cout << "A::func" << endl; }
};

struct B : public A
{
    void func() { cout << "B::func" << endl; }
};

typedef void (*functionPtr)();

int main()
{
    A *b = new B();

    int *vptr = *(int**)&b;
    int *vtable = (int *)*vptr; 

    functionPtr fp = (functionPtr)vtable[0];

    printf("address A::func = %p\n", fp);

    return 0;
}
hidayat