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 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
  • 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-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

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have to pass the form data to the next… May 13, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer When you pressed CMD F on older versions of Xcode… May 13, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer echo 2222 | grep -E '2{2}' 2222 The regex will… May 13, 2026 at 11:44 pm

Related Questions

Usually, when we have to link an interface element to a field of a
One-line summary: What is the best practice for unhooking event handlers created in the
I have an idea for how to solve this problem, but I wanted to
I am still very new to Ruby (reading through the Pickaxe and spending most
I've read some of the questions regarding anemic domain models and separation of concerns.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.