Solved

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

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

Thanks!
0
Comment
Question by:gromul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

732 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