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 9168685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:41:04+00:00 2026-06-17T15:41:04+00:00

There are all kinds of cases from simple to complex structures of virtual functions.

  • 0

There are all kinds of cases from simple to complex structures of virtual functions. What factor determines the number of extra memory it required? For example

class A            {virtual void F() {}   };
class B : public A {virtual void F() {}   };

How many more memory does A and B require compare to classes without virtual functions? Another example with 2 virtual functions,

class A            {virtual void F() {}   virtual void G() {}   };
class B : public A {virtual void F() {}   virtual void G() {}   };

and example 3

class A            {virtual void F() {};   virtual void G() {}  };
class B : public A {virtual void F() {}                         }; 

[————— Add More Interesting questions ——————]

  1. Does every virtual method have a virtual method table? If so, I think for a class (not object, right? I think the VPTR is static.) with a number of virtual method requires a number of VPTRs, each for a virtual method. Right?

  2. Is it possible to build just one table for all virtual methods and put only one VPTR in a class?

  3. And I think the calling speed of virtual functions should be faster than manual ifs since the direct VPTR is used. Right?

[————— A Test —————-]

I did a test and show my results with both VS2010 and intel c++ compiler.

struct A
{
    static int s_i;
    int i;
    virtual void F()    {i+=1;}
    virtual void G()    {i+=2;}
};

struct B
    : public A
{
    int j;
    virtual void F()    {i+=3;}
    virtual void G()    {i+=4;}

    virtual void H()    {i+=5;}
};

struct C
    : public B
{
    virtual void F()    {i+=6;}

    virtual void H()    {i+=7;}
};

TEST(MemoryForVirtualMethod)
{
    CHECK_EQUAL(sizeof(A), 8);
    CHECK_EQUAL(sizeof(B), 12);
    CHECK_EQUAL(sizeof(C), 12);
}

From the results, my conclusions are

(1) For each object with virtual functions, one (hidden pointer of) VPTR is added.

(2) Foe each class, a virtual method table is built for all virtual methods of the class.

(3) Putting the VPTR in object is to implement the dynamic dispatch since the class referred may not be the dynamic class.

(4) The implementation is efficient in calling (faster than manual ifs) but sacrifices some memory.

Thanks a lot!

  • 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-17T15:41:05+00:00Added an answer on June 17, 2026 at 3:41 pm

    Since virtual functions are typically implemented as a virtual method table (vtable), I am guessing O(number of virtual functions) per class and 1 extra pointer per object. Not as exact as you want, but should give a rough idea.
    As @AlokSave mentions, there are many other subtleties which are very compiler specific and may not be consistent enough to estimate.

    Typically, the compiler creates a separate vtable for each class. When an object is created, a pointer to this vtable, called the virtual table pointer, vpointer or VPTR, is added as a hidden member of this object

    Source : Wikipedia

    The number of functions you override (as in your examples) is (im guessing) irrelevent, except when the function to be called can safely be determined at compile time, in which case it might be treated as a static dispatch function (G() in your 2nd example). This is also up to the compiler.

    compilers usually avoid using vtables whenever the call can be resolved at compile time.

    Finally, The cost of virtual function is likely to be harder on speed than memory..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a system that does all kinds of complex and simple select queries.
What all possible support is there from open source world for building web based
Assuming there are buttons with all the letters from the alphabet set as Text.
First of all there is probably a question like this already but i couldn't
Hi All is there any way to locally define a variable in a function
I created a virtualenv in my django app folder, and there I installed all
There are many things that all programmers should know, but I am particularly interested
There is a rewrite rule ensuring that all URL's head to index.php and maintain
There is any ways to listing all main directories present in php server(may it
There is a term named API communication all over around. Can anybody tell me

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.