Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:31:06+00:00 2026-06-01T07:31:06+00:00

I was recently reading an article in the bitsquid blog about how to manage

  • 0

I was recently reading an article in the bitsquid blog about how to manage memory and the author began to talk about the vtable and how there is a pointer added to the class by the compiler. Here is the link for the article. So because I hardly knew anything about the vtalbe I began to search the web for an explanation. I came accross this link. Based on what I read I made the following code:

char cache[24];

printf("Size of int = %d\n", sizeof(int));
printf("Size of A = %d\n", sizeof(A));

A* a = new(cache)A(0,0);
printf("%s\n",cache);
printf("vTable    : %d\n",*((int*)cache));
printf("cache addr: %d\n",&cache);

int* funcPointer = (int*)(*((int*)cache));
printf("A::sayHello: %d\n",&A::sayHello);
printf("funcPointer: %d\n",*funcPointer);

A is a class with two integers members and a virtual function sayHello().

EDIT : Here is the class definition:

class A {
public:
    int _x;
    int _y;
public:
    A(int x, int y) : _x(x), _y(y){ }
    virtual void sayHello() { printf("Hello You!"); }
};

Basically what I was trying to do is see if the pointer inside the vtable would point to the same place as where the address I get from &A::sayHello, but the thing is that when I run the program, the address inside the pointer in the vtable and the sayHello() address always has a difference of 295. Does anyone know why this could be happening? Is there some kind of header that’s added that I’m missing? I’m running visual studio express 2008 on a 64 bit machine.

From what I have debugged the address that is returned by *funcPointer is the true address of the function sayHello(). But why is the &A::sayHello() returning a different address?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:31:07+00:00Added an answer on June 1, 2026 at 7:31 am

    C++ has an interesting feature :

    If you take a pointer to a virtual function and use it, the virtual function will be resolved and called.

    Let’s take a simple example

    struct A
    {
        virtual DoSomething(){ printf("A"); }
    };
    
    struct B: public A
    {
        virtual DoSomething() { printf("B"); }
    };
    
    void main()
    {
        A * a, b;
        void (A::*pointer_to_function)();
    
        pointer_to_function = &A::DoSomething;
        a = new A;
        b = new B;
    
        (a.*pointer_to_function)() //print "A"
        (b.*pointer_to_function)() //print "B"
    }
    

    So, the address you see using &A::DoSomething is the address of the trampoline, not the real function address.

    If you go to assembly, you’ll see that function does something like that (register may change but ecx which represent the this pointer ):

    mov eax, [ecx] ; Read vtable pointer
    lea edx, [eax + 4 * function_index ] ; function_index being the index of function in the vtable
    call edx  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading an article about the recently released Gizzard sharding framework by twitter (
Recently I was reading about Article on Interviewing an Software Engineering Position by Joel
I was recently reading about storing floating point values in the memory. And I've
I was recently reading an article on the Windows Metafile vulnerability (http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability#Third-party_patch) and I
I’ve recently started learning IOCP on Windows and been reading the following article: http://www.codeproject.com/Tips/95363/Another-TCP-echo-server-using-IOCP
I recently was reading about Oracle Index Organized Tables (IOTs) but am not sure
I recently started reading about ASP.net MVC and after getting excited about the concept,
I've been reading recently about DI and IoC in C++. I am a little
I have recently started reading about dependency injection and it has made me rethink
Recently I was reading about Codd's 12 Rules , and I understood all except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.