Solved

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

Posted on 2006-07-11
3
650 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 71
Socket Programming (Unix) 8 128
best sources to up-to-date in C++? 8 82
Copy output image from TWindowsMediaPlayer 6 41
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

778 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