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

  • Home
  • SEARCH
  • 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 656839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:45:29+00:00 2026-05-13T22:45:29+00:00

When we create an object of a class what does it memory map look

  • 0

When we create an object of a class what does it memory map look like. I am more interested in how the object calls the non virtual member functions. Does the compiler create a table like vtable which is shared between all objects?

class A
{
public:
  void f0() {}
  int int_in_b1;
};

A * a = new A;

What will be the memory map of a?

  • 1 1 Answer
  • 1 View
  • 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-05-13T22:45:29+00:00Added an answer on May 13, 2026 at 10:45 pm

    You can imagine this code:

    struct A {
      void f() {}
      int int_in_b1;
    };
    
    int main() {
      A a;
      a.f();
      return 0;
    }
    

    Being transformed into something like:

    struct A {
      int int_in_b1;
    };
    void A__f(A* const this) {}
    
    int main() {
      A a;
      A__f(&a);
      return 0;
    }
    

    Calling f is straight-forward because it’s non-virtual. (And sometimes for virtual calls, the virtual dispatch can be avoided if the dynamic type of the object is known, as it is here.)


    A longer example that will either give you an idea about how virtual functions work or terribly confuse you:

    struct B {
      virtual void foo() { puts(__func__); }
    };
    struct D : B {
      virtual void foo() { puts(__func__); }
    };
    
    int main() {
      B* a[] = { new B(), new D() };
      a[0]->foo();
      a[1]->foo();
      return 0;
    }
    

    Becomes something like:

    void B_foo(void) { puts(__func__); }
    void D_foo(void) { puts(__func__); }
    
    struct B_VT {
      void (*foo)(void);
    }
    B_vtable = { B_foo },
    D_vtable = { D_foo };
    
    typedef struct B {
      struct B_VT* vt;
    } B;
    B* new_B(void) {
      B* p = malloc(sizeof(B));
      p->vt = &B_vtable;
      return p;
    }
    
    typedef struct D {
      struct B_VT* vt;
    } D;
    D* new_D(void) {
      D* p = malloc(sizeof(D));
      p->vt = &D_vtable;
      return p;
    }
    
    int main() {
      B* a[] = {new_B(), new_D()};
      a[0]->vt->foo();
      a[1]->vt->foo();
      return 0;
    }
    

    Each object only has one vtable pointer, and you can add many virtual methods to the class without affecting object size. (The vtable grows, but this is stored once per class and is not significant size overhead.) Note that I’ve simplified many details in this example, but it does work: destructors are not addressed (which should additionally be virtual here), it leaks memory, and the __func__ values will be slightly different (they’re generated by the compiler for the current function’s name), among others.

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

Sidebar

Related Questions

I am curious, if I create a class of many methods (functions as PHP
I'm thinking of using pure/const functions more heavily in my C++ code. ( pure/const
Say i create a derived class as below, class CHIProjectData : public QObject {
I am trying to figure out the memory consumption by my (C++) program using
I'm trying to use HashSet to store objects of a class that I created,
I was intrigued by the answer to a similar question. I believe it is
I'm supposed to do the fallowing: 1) read a huge (700MB ~ 10 million
I have a serious question for you guys. I am working on a project
Okay so I have two import pieces of code involved in this. This first
Consider the following: var o = new { Foo = foo, Bar = bar

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.