473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initialization of virtual function table pointer

I was recently looking at some assembly code generated by my compiler. When
examining the assembly code associated with constructors I noticed that the
dynamic-dispatch mechanism was enabled before member initialization but after
base initialization.

To speak in implementation details the virtual function table pointer for
objects being constructed was set before member initialization but after base
initialization.

Curiousity then led me to the following toy program...

<CODE>

struct Base
{
Base(int) {}
};

class Derived : public Base
{
public:

Derived():
Base(ProxyForVirtualFunction()),
member(ProxyForVirtualFunction())
{
}

private:

int ProxyForVirtualFunction()
{
return VirtualFunction();
}

virtual int VirtualFunction()
{
return 0;
}

int member;
};

int main()
{
Derived();

return 0;
}

</CODE>

In the example code class "Derived" has a base class "Base" which must be
initialized with an integer. It also has an int member that is to be
initialized. In both cases the value used for initialization is produced by
indirectly calling a virtual function. The reason for the indirect call is
because my compiler optimizes away direct calls to virtual functions within
constructors.

The call sequence that produces the value to be used as the base class
initializer results in a program crash because the virtual function table
pointer has not yet been set. The attempt to dispatch the virtual function
call accesses an out-of-bounds memory location and all hell breaks loose.

The call sequence that produces the value to be used as the member initializer
results in no fault because at that point the virtual function table pointer
has been set.

I am wondering if the C++ language standard states when a dynamic dispatch
mechanism is expected to be associated with an object under construction and
what assurances the language standard makes regarding when it is okay to assume
that dynamic dispatch is safe.

Any insight would be appreciated.

Jul 22 '05 #1
4 2690
DaKoadMunky wrote:

I am wondering if the C++ language standard states when a dynamic dispatch
mechanism is expected to be associated with an object under construction and
what assurances the language standard makes regarding when it is okay to assume
that dynamic dispatch is safe.

Any insight would be appreciated.


All member functions called from constructor are statically linked what
implies that dynamic dispatch is not used in that case. It is easy to
imagine the disastrous effect when an overriding function tried to
access a member that was not initialized yet.

For more information check:

http://www.parashift.com/c++-faq-lit....html#faq-23.3
Jul 22 '05 #2
>All member functions called from constructor are statically linked

I am aware of that. That is why my example called the virtual functions
indirectly through a non-constructor function where such calls would not be
statically linked.
It is easy to imagine the disastrous effect when an overriding function tried toaccess a member that was not initialized yet.


Yes it is. In fact I have been studying Java recently and was surprised to
discover that in that language derived class overrides are invoked when called
from base class constructors. After thinking about it though I am of the
opinion that can be a useful feature if the appropriate cautions are taken and
the author of the base class documents that a dynamically dispatched method is
being called from a base class constructor. I guess that is an argument for
another time though.
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #3
DaKoadMunky wrote:

Yes it is. In fact I have been studying Java recently and was surprised to
discover that in that language derived class overrides are invoked when called
from base class constructors.

In Java all data members are zero initialized before any contructor code
gets called. It is also up to the programmer to specify if a base class
constructor should be called. So there is more control and no risk of
using an unitialized data member. At least not bigger than using one
with a dummy value. So potentially confusing object construction rules
applied in C++ do not have to be reflected in Java.

Jul 22 '05 #4
Janusz Szpilewski posted:
DaKoadMunky wrote:

Yes it is. In fact I have been studying Java recently and was
surprised to discover that in that language derived class overrides
are invoked when called from base class constructors.

In Java all data members are zero initialized before any contructor
code gets called.

Better to have the choice.
It is also up to the programmer to specify if a base
class constructor should be called. So there is more control and no
risk of using an unitialized data member. At least not bigger than
using one with a dummy value. So potentially confusing object
construction rules applied in C++ do not have to be reflected in Java.

I agree that dumbing down _can_ make it easier for people who aren't
professional. But then you have professional programmers, who don't want to
work with a Virtual Machine, reminiscent of Visual Basic.

And if a Derived class does _not_ call the constructor of the Base class,
then you can't really say that it's a derived class at all. Is a dog a
mammal if it hasn't even been conceived, born, breast-fed, weined... I don't
think so.

-JKop
Jul 22 '05 #5

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

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
7
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has...
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.