473,769 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is vtable exactly?is it a structure or an array?

Hello All,
I have read in many c++ literature that vtable is nothing but an array
of pointer to virtual functions inside a class.And the class where the
virtual function/s are declared stores the vfptr i.e the pointer to
vtable internally.
What confuses me is
if vtable is an array of pointer to virtual functions then as per the
properties of the array all the entries inside the array must be
same.i.e all the pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as they must
have same singnature as per my point.

eg. class base
{
public:
virtual void fun(int,int);
virutal int fun2(char);
};
now according to the definition of vtable(i.e array of pointers to
virtual functions)
what will be the syntax for the array? as single array can't hold
entries with different datatypes.
the pointer to first virtual function is something like
void (base::*fptr1)( int,int) and for second its like
int (base::*fptr2)( char)
how come a single array like vtable will stored these two totally
different entires?

but a structure can stores mulitple datatypes.
So is Vtable a structure or an array? if its array then can anyone give
me its definition?

Thanks and regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 3 '06 #1
17 14717
yp*********@ind iatimes.com wrote:
Hello All,
What confuses me is
if vtable is an array of pointer to virtual functions then as per the
properties of the array all the entries inside the array must be
same.i.e all the pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as they must
have same singnature as per my point.


1. C++ std doesnot say anything about implementation of virtual
functions. The compilers are free to implement it by any (correct)
mechanism. (However, practically majority of the (or is it 'all' ? )
compilers that exist today implement virtual functions using vtable
mechanism.)

2. The type restrictions that you are talking about exist for a
programmer when he wants to write a C++ code which will get
type-checked during compilation. Such restrictions donot exist for the
compiler. Specifically, the compiler doesnot generate a C++ code
corresponding to array of function pointers - it simply spits out the
assembly code corresponding to indexing into an array of addessses. So
the issues of type-checking donot really come in the picture.

Of couse, I have not really seen the implemenation and so I might not
be accurate. Comments are welcome.

Jan 3 '06 #2

yp*********@ind iatimes.com wrote:
Hello All,
I have read in many c++ literature that vtable is nothing but an array
of pointer to virtual functions inside a class.And the class where the
virtual function/s are declared stores the vfptr i.e the pointer to
vtable internally.


The v-table is beyond C++. You cannot access it directly, or get a
pointer to a v-table.

The writers of the compiler may well implement it the same way they
implement an array of function pointers and the C++ literature that
indicates as such may either be aimed at compiler-writers or may be
illustrating how one could simulate a v-table (eg if coding in another
language such as C).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 3 '06 #3
yp*********@ind iatimes.com wrote:
I have read in many c++ literature that vtable is nothing but an array
of pointer to virtual functions inside a class.And the class where the
virtual function/s are declared stores the vfptr i.e the pointer to
vtable internally.
Not to give you yet another piece of C++ literature to reference, but I
found Stroustrup's explanation in "The Design and Evolution of C++"
pretty straightforward .
What confuses me is
if vtable is an array of pointer to virtual functions then as per the
properties of the array all the entries inside the array must be
same.i.e all the pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as they must
have same singnature as per my point.

but a structure can stores mulitple datatypes.
So is Vtable a structure or an array? if its array then can anyone give
me its definition?


I think you're getting confused because you're trying to carry over
rules about the C++ language to the domain of compiler implementation
and runtime architecture. The vtable is an area in memory. Call it an
array if you want. C++ doesn't let you allocate heterogeneous arrays
directly, but you can always get around this if you really want to.
Just allocate a chunk of memory, manage your pointers very carefully,
cast if ya gotta, and you really have complete control. But "you" in
this case is the compiler implementor, who may or may not even be using
C++.

The point is, it's implementation-defined. All that's required is that
some mechanism be provided that supports the virtualization behaviour
required in the standard. And that's all you should count on, or worry
about, unless you have a reason to delve into compiler internals or the
like.

Luke
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 3 '06 #4
yp*********@ind iatimes.com wrote:
Hello All,
What confuses me is
if vtable is an array of pointer to virtual functions then as per the
properties of the array all the entries inside the array must be
same.i.e all the pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as they must
have same singnature as per my point.


1. C++ std doesnot say anything about implementation of virtual
functions. The compilers are free to implement it by any (correct)
mechanism. (However, practically majority of the (or is it 'all' ? )
compilers that exist today implement virtual functions using vtable
mechanism.)

2. The type restrictions that you are talking about exist for a
programmer when he wants to write a C++ code which will get
type-checked during compilation. Such restrictions donot exist for the
compiler. Specifically, the compiler doesnot generate a C++ code
corresponding to array of function pointers - it simply spits out the
assembly code corresponding to indexing into an array of addessses. So
the issues of type-checking donot really come in the picture.

Of couse, I have not really seen the implemenation and so I might not
be accurate. Comments are welcome.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 3 '06 #5
Neelesh Bodas wrote:
1. C++ std doesnot say anything about implementation of virtual
functions. The compilers are free to implement it by any (correct)
mechanism. (However, practically majority of the (or is it 'all' ? )
compilers that exist today implement virtual functions using vtable
mechanism.)

2. The type restrictions that you are talking about exist for a
programmer when he wants to write a C++ code which will get
type-checked during compilation. Such restrictions donot exist for the
compiler. Specifically, the compiler doesnot generate a C++ code
corresponding to array of function pointers - it simply spits out the
assembly code corresponding to indexing into an array of addessses. So
the issues of type-checking donot really come in the picture.


Except when it spits out something else, of course. The main reason for
that is pointer adjustment. Calling a Base method with a Derived* is
legal
but in assembly you might need to add or subtract a few bytes to get
the Base* needed. This is worse when Base is a virtual base class.
These
adjustments typically end up in the vtable as well (since it's already
in
the CPU cache at this point, or is needed very soon)

A good compiler might also spit out a few guard entries at the end, so
that
any (forced) attempt to use a Derived method with a Base* is caught.
And
of course many compilers use similar stubs to deal with pure virtual
functions, so a call from the ctor fails predictably.

A compiler also can mix different pointer sizes. It only needs to map
virtual void foo() to a byte offset in the vtable.If there's a 32-bit
pointer
and a 64-bit pointer in there, it'll probably use offset 6. Arrays
don't work
that way :)

HTH,
Michiel Salters

Jan 3 '06 #6
yp*********@ind iatimes.com wrote:
I have read in many c++ literature that vtable is nothing but
an array of pointer to virtual functions inside a class. And
the class where the virtual function/s are declared stores the
vfptr i.e the pointer to vtable internally. What confuses me is if vtable is an array of pointer to
virtual functions then as per the properties of the array all
the entries inside the array must be same. i.e all the
pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as
they must have same singnature as per my point.


The vtable (or vtbl) is part of what the compiler generates, and
not a C++ structure. On most machines, at the hardware level, a
pointer is a pointer, regardless of what it points to. And the
vtable is an array of these banalized pointers -- in a modern
implementation, typically one of the entries will not point to a
function at all, but to the data structures used for RTTI
(typeid and dynamic_cast). When a document says that the vtable
is an array of pointers to the virtual functions, it doesn't
mean that this is the type of the array, as in C++; it simply
means that (most of) the actual banalized pointers in the array
happen to point to the virtual functions.

If the C++ compiler is generating C code, then it has two
alternatives: it can generate a struct for each class, and use
that, or it can generate code as if it were an array of some
generic function pointer type, e.g. void (*)(), and cast as
needed.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 4 '06 #7
yp*********@ind iatimes.com wrote:
[snip]
So is Vtable a structure or an array?

[snip]

The short answer: MYOB. It's implementation defined and not explicitly
accessible to your C++ program. See this FAQ (and the preceding one)
for some details:

http://www.parashift.com/c++-faq-lit....html#faq-20.4

Cheers! --M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 4 '06 #8
every pointers has exactly same size
a pointer array usually has a type of void *
it can contain every kind of pointers
so ...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 4 '06 #9
Earl Purple wrote:
yp*********@ind iatimes.com wrote:
Hello All,
I have read in many c++ literature that vtable is nothing but an array
of pointer to virtual functions inside a class.And the class where the
virtual function/s are declared stores the vfptr i.e the pointer to
vtable internally.


The v-table is beyond C++. You cannot access it directly, or get a
pointer to a v-table.

That is not 100% true. One could figure out how a particular compiler
implements the v-table and access it using various casts and pointer
arithmetic. Not that I would suggest doing such a thing, but it is
possible nonetheless.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2124
by: Chang Byun | last post by:
Hi, folks, I have a question about structure array as a argument. The below short program is that main function call a subroutine which increases real and imaginary part by 1 respectively in complex numbered structure array. There is no error when it is compiled. But when it is running, it has a segmentation fault.
1
6446
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float myvar;
5
6587
by: s88 | last post by:
Howdy: the follows is my program, I wanna change my structure array pointer in the function "testfunc", but I fail..., I also try to call the testfunc by reference, but the compiler says "test6.c:35: error: incompatible type for argument 1 of `testfunc'". Can I make my purpose in C? and how? typedef struct xxxx *xxxx_ptr;
2
7211
by: Charles Law | last post by:
Does anyone know if it is possible to initialise a structure array at run-time, something like this: <code> Structure struct Dim a As String Dim b As Integer Dim c As End Structure
0
1515
by: lov4mu6 | last post by:
Hello, I have a DLL that I've compiled in Visual C++ which I'm referencing in VB .NET The function in the DLL returns a simple structure array. I need to be able to capture this structure array in VB .NET. C++ code: struct NotesInfo
0
1523
by: JoeyB | last post by:
Hey All, My app will have a structure array that will hold the parameters used to initialize a array of classes at run time to default values. XML looks like a good candidate to persist the data right? I'd like to have the XML file that persists the structure array. It would be nice to just initially author a XML file with my parameter values in it. It's a bit of a chicken/egg senerio. Is there any way to do this without first...
12
2269
by: estantep | last post by:
Hello, I am having some trouble trying to pass a pointer to an structure array (as function argument), when executing I get seg faults. The best I could get out from googling was: #define MAX_GRAPH 256 typedef struct edge{
5
3796
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.