473,782 Members | 2,505 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
17 14718
On 4 Jan 2006 09:24:59 -0500, "Meador Inge" <me*****@gmail. com> wrote:
Earl Purple wrote:
yp*********@ind iatimes.com wrote:
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.


That is how Microsoft COM works. It "walks" the vtable. IIRC COM is an
external process so it has to get hold of the vtable just like any
programmer would have to. It helps a great deal to know how this
mechanism works.

To the OP, ignore people who tell you to "MYOB" or it's out of your
control so don't bother. People like that, regardless of their title
and/or education, are NOT engineers, software or otherwise.

Not all those who wander are lost. - J.R.R. Tolkien
Jan 4 '06 #11
JustBoo wrote:
On 4 Jan 2006 09:24:59 -0500, "Meador Inge" <me*****@gmail. com> wrote:
Earl Purple wrote:
yp*********@ind iatimes.com wrote:
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.


That is how Microsoft COM works. It "walks" the vtable. IIRC COM is an
external process so it has to get hold of the vtable just like any
programmer would have to. It helps a great deal to know how this
mechanism works.

To the OP, ignore people who tell you to "MYOB" or it's out of your
control so don't bother. People like that, regardless of their title
and/or education, are NOT engineers, software or otherwise.


In my response, I wrote:

"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.c om/c++-faq-lite/virtual-functions.html# faq-20.4"

Since your comments are at least directed at me, please allow me to
respond.

The "MYOB" in my response was half joking. Of course, in some
relatively rare scenarios it might be necessary to figure out how a
compiler implements virtual functions and make use of that information
(n.b., compilers aren't required to use vtables at all and can
implement virtual functions with some other mechanism). However, as I
said, the language does not make that information *explicitly*
available to the user, and so the user *must* do some
platform-dependent pointer manipulation to figure out where the vtable
is and how to use it (assuming it exists at all!).

Therefore, since this newsgroup is exclusively concerned with
*standard* C++ language and libraries
(http://www.parashift.com/c++-faq-lit....html#faq-5.9), such
manipulations are, according to the FAQ, off-topic here and should be
taken to a more specific newsgroup that can treat them more adequately.

As far as implementing COM, Microsoft is free to make use of
non-standard information because it also controls the compiler. Another
company creating a COM-esque technology for some third-party
compiler(s) might pull the same sort of non-standard tricks, only to
find out on the next release of their compiler(s) that their tricks for
manipulating the vtable no longer work. That is because they used a
non-standard implementation detail from one version of their compiler
that the Standard allows the compiler vendor to change at will without
becoming non-conformant.

Finally, with regard to the aspersions and calumnies cast about in your
last sentence, I would humbly recommend that you dismount from your
elevated equine.

Cheers! --M

Jan 4 '06 #12

Dudle.June wrote:
every pointers has exactly same size


Not true in general.
a) On word addressed machines, void* and (unsigned) char * are longer
than other pointers because they contain the offset within the word.
b) In segmented architectures (like 16-bit Intel) it was quite common
for pointers to data and pointers to functions to be different lengths.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 4 '06 #13
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Dudle.June <du*****@gmail. com> writes
every pointers has exactly same size
a pointer array usually has a type of void *
it can contain every kind of pointers
so ...

Actually your premise is false. Pointers to different types are not
required to be the same size. In addition a void* is not a correct
pointer type for a function pointer. When all is said and done compilers
implement late binding by creating internal tables of addresses, one per
class in a polymorphic hierarchy. The compler ensures that the same
function signature is applicable to the same slot in each of the tables.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 5 '06 #14
May anyone tell me what exactly the pointer is ?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 5 '06 #15

<wi************ @gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
May anyone tell me what exactly the pointer is ?


Which pointer? If you really meant to ask, what *a* pointer is,
it's an obect or value which represents a memory address.

-Mike
Jan 5 '06 #16

wi************@ gmail.com wrote:
May anyone tell me what exactly the pointer is ?


No. We can't. It depends on a lot of factors, and we don't have a
crystal ball.
In fact, the next time you compile the profiler-optimizer may decide
not to
use a pointer at all.

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

Jan 6 '06 #17
Hello All,

Thanks for all your help.
This discussion really help me understand the concept of the virtual
functions mechanisms.

Actually the reason behind asking this question is exactly what JustBoo
pointed out.I was loooking for interoperatibil y behind the c++ and C
when using the COM objects and how to access c++ vfptr from C..

I really appreciate all your help and guidance..
Hoping to get more help from this community in future..

Thanks and Regards,
Yogesh Joshi

Jan 9 '06 #18

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
7212
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
1516
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
1526
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
2270
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
9639
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
10311
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...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
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...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
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...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.