Solved

Draw the memory layout of a C++ class with two virtual functions

Posted on 2006-07-11
3
655 Views
Last Modified: 2008-01-09
It's an interview question. Any help is appreciated.

Thanks!
0
Comment
Question by:gromul
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17085339
See http://en.wikipedia.org/wiki/Virtual_method_table

Assuming

class foo {

public:

virtual void f1();
virtual void f2();

};

foo:
  +0: pointer to virtual method table of foo

Basically, it is only the pointer to the VTABLE if the class has no other (data) members - 4 bytes, that's it.

If you have

class foo {

public:

virtual void f1();
virtual void f2();

int n;
};

that'd be

  +0: pointer to virtual method table of foo
  +4: value of 'n'
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17085513
This is only a possible implementation, though it's extremely common, so I'd avoid saying this is THE layout.
It's conceivable that on some weird architecture they could do something else.

Good luck
0
 
LVL 4

Expert Comment

by:havman56
ID: 17097381
JKR  thanks for the link fom wikipedia

i learnt lot abt vtable  !!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question