Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   type_info, vtable (http://www.velocityreviews.com/forums/t276870-type_info-vtable.html)

sks_cpp 08-19-2003 01:50 AM

type_info, vtable
 
What is a type_info function, more particularly, what is a type_info node?
Are these related to the vtable by any means?
I have seen linker errors such as:
"undefined reference to SomeClass type_info node" or
"undefined reference to SomeClass type_info function"

What does that really mean? What is the linker looking for?

Thanks.



Ivan Vecerina 08-19-2003 08:17 AM

Re: type_info, vtable
 
"sks_cpp" <sksjava@hotmail.com> wrote in message
news:Nnf0b.32424$Ij4.31849@news2.central.cox.net.. .
| What is a type_info function, more particularly, what is a type_info node?
| Are these related to the vtable by any means?

Somewhat. In several implementations, the vtable stores the address of
the type_info data structure.
This data structure is used to implement Run-Time Type Identification
(RTTI),
a feature which is required for exception handling, dynamic_cast calls,
and the implementation of the typeid operator (see header <typeinfo>).

| I have seen linker errors such as:
| "undefined reference to SomeClass type_info node" or
| "undefined reference to SomeClass type_info function"

Some C++ compiler implementations allow to enable/disable support
for RTTI. You probably need to enable it for some files...


hth
--
Ivan Vecerina <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com





Rolf Magnus 08-19-2003 09:32 AM

Re: type_info, vtable
 
Ivan Vecerina wrote:

> This data structure is used to implement Run-Time Type Identification
> (RTTI), a feature which is required for exception handling,


RTTI isn't need for exception handling, neither would it be sufficient.


Ivan Vecerina 08-19-2003 01:29 PM

Re: type_info, vtable
 
"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:bhsr18$crk$03$1@news.t-online.com...
| Ivan Vecerina wrote:
|
| > This data structure is used to implement Run-Time Type Identification
| > (RTTI), a feature which is required for exception handling,
|
| RTTI isn't need for exception handling, neither would it be sufficient.

I did not claim it was sufficient.

But RTTI is needed to match catch handlers with exceptions.
Consider:

#include <vector>
#include <string>
#include <iostream>
#include <exception>
int main()
{
try {
std::vector<std::string> tooBig(1UL<<31,"just some string");
}
catch(std::exception& x) {
std::cerr << "Failure: "<<x.what()<<std::endl;
}
}

How would the C++ run-time handle a failure of this memory allocation
without RTTI?


Regards,
Ivan
--
http://www.post1.com/~ivec



Alf P. Steinbach 08-19-2003 01:41 PM

Re: type_info, vtable
 
On Tue, 19 Aug 2003 15:29:37 +0200, "Ivan Vecerina" <ivec@myrealbox.com> wrote:

>Consider:
>
> #include <vector>
> #include <string>
> #include <iostream>
> #include <exception>
> int main()
> {
> try {
> std::vector<std::string> tooBig(1UL<<31,"just some string");
> }
> catch(std::exception& x) {


Preferably use

catch( std::exception const& x )


> std::cerr << "Failure: "<<x.what()<<std::endl;
> }
> }
>
>How would the C++ run-time handle a failure of this memory allocation
>without RTTI?


The actual types are not needed at run-time, only the compatibility
matrix of catches and throws.

That abstract matrix can be optimized in umpteen ways.

One way, but not the only way, is to use RTTI.


Alf P. Steinbach 08-20-2003 09:28 AM

Re: type_info, vtable
 
On Tue, 19 Aug 2003 19:48:09 +0200, "Ivan Vecerina" <ivec@myrealbox.com> wrote:

>"Alf P. Steinbach" <alfps@start.no> wrote in message
>news:3f4226d5.414481453@News.CIS.DFN.DE...
>| On Tue, 19 Aug 2003 15:29:37 +0200, "Ivan Vecerina" <ivec@myrealbox.com>
>wrote:
>| > try {
>| > std::vector<std::string> tooBig(1UL<<31,"just some string");
>| > }
>| > catch(std::exception& x) {
>|
>| Preferably use
>|
>| catch( std::exception const& x )
>Agreed.
>
>| > std::cerr << "Failure: "<<x.what()<<std::endl;
>| > }
>| > }
>| >
>| >How would the C++ run-time handle a failure of this memory allocation
>| >without RTTI?
>|
>| The actual types are not needed at run-time, only the compatibility
>| matrix of catches and throws.
>|
>| That abstract matrix can be optimized in umpteen ways.
>|
>| One way, but not the only way, is to use RTTI.
>
>Actually, the exact same thing can be said of dynamic_cast:
>you only need a compatibility matrix of source and destination types.


I see where the confusion stems from now.

No, dynamic cast is a very different. With 'throw' the compiler knows
the exact type of the argument. With 'dynamic_cast' the argument can
be a pointer to an object that according to the standard must have its
dynamic type checked (a pointer to 'throw' is not checked for dyntype).



>Clearly, Exception Handling does require a form of run-time type
>identification (albeit this RTTI does not formally need to
>rely on a data structure known to the linker as 'type_info').


Nope.




>Let's just put things in the context of the OP, who was requesting
>information on why his linker reported missing 'type_info' nodes.
>I explained which language features may "require" them, and that
>enabling RTTI is probably the way to have them generated.
>
>Was this incorrect?


That I don't know...


Risto Lankinen 08-20-2003 09:29 AM

Re: type_info, vtable
 

"Alf P. Steinbach" <alfps@start.no> wrote in message
news:3f4226d5.414481453@News.CIS.DFN.DE...
> On Tue, 19 Aug 2003 15:29:37 +0200, "Ivan Vecerina" <ivec@myrealbox.com>

wrote:
> >
> >How would the C++ run-time handle a failure of this memory allocation
> >without RTTI?

>
> The actual types are not needed at run-time, only the compatibility
> matrix of catches and throws.
>
> That abstract matrix can be optimized in umpteen ways.
>
> One way, but not the only way, is to use RTTI.


Furthermore, exception handling uses compile-time types,
not run-time (e.g. dynamic) types. Try this example:

#include <iostream.h>

struct Base
{
virtual ~Base()
{
}
};

struct Derived : public Base
{
};

int main()
{
Base *p = new Derived;

try
{
throw *p;
}
catch( const Derived & )
{
cout << "Derived" << endl;
}
catch( const Base & )
{
cout << "Base" << endl;
}
catch( ... )
{
cout << "<unknown>" << endl;
}

delete p;

return 0;
}

This program prints "Base".

Cheers!

- Risto -




Ivan Vecerina 08-20-2003 02:30 PM

Re: type_info, vtable
 
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:3f433e86.486081828@News.CIS.DFN.DE...
| On Tue, 19 Aug 2003 19:48:09 +0200, "Ivan Vecerina" <ivec@myrealbox.com>
wrote:
| >"Alf P. Steinbach" <alfps@start.no> wrote in message
| >news:3f4226d5.414481453@News.CIS.DFN.DE...
| >| The actual types are not needed at run-time, only the compatibility
| >| matrix of catches and throws.
| >|
| >| That abstract matrix can be optimized in umpteen ways.
| >|
| >| One way, but not the only way, is to use RTTI.
| >
| >Actually, the exact same thing can be said of dynamic_cast:
| >you only need a compatibility matrix of source and destination types.
|
| I see where the confusion stems from now.
|
| No, dynamic cast is a very different. With 'throw' the compiler knows
| the exact type of the argument.

Yes, just as when a new object is created (e.g. with the 'new' operator).

| With 'dynamic_cast' the argument can
| be a pointer to an object that according to the standard must have its
| dynamic type checked (a pointer to 'throw' is not checked for dyntype).

And when a catch clause is encountered during stack unwinding, the
dynamic type of the exception being thrown (and eventually re-thrown)
needs to be matched with the type of the catch clause.

For dynamic_cast as well, the compiler could make some static program
analysis and establish that the provided object has to be of a specific
type. The fact is, in practice, this would be too difficult.
And it would be about as difficult to pre-match try and catch clauses.

When compiling a throw clause, the catch handlers that will be on the
call stack are not known. Reciprocally, when compiling a try-catch block,
the throw statements that may be invoked are unknown. So the matching
of these two types somehow has to happen at run-time.

For dynamic_cast, RTTI involves the matching of a type identifier
which is defined during object creation (and typically stored within
the vtbl of an object, with a locally known type.
For Exception Handling, RTTI involves the matching of a type identifier
somehow defined at an unknown throw point, with the locally known
type of a catch clause (or reciprocally).
It just isn't that much different. And as far as I know (from the last
time I checked the internals of a C++ runtime core), the same kind of
type information is typically used to implement both these mechanisms.


Kind regards,
Ivan
--
http://www.post1.com/~ivec



Alf P. Steinbach 08-20-2003 02:50 PM

Re: type_info, vtable
 
On Wed, 20 Aug 2003 16:30:22 +0200, "Ivan Vecerina" <ivec@myrealbox.com> wrote:

>"Alf P. Steinbach" <alfps@start.no> wrote in message
>news:3f433e86.486081828@News.CIS.DFN.DE...
>| On Tue, 19 Aug 2003 19:48:09 +0200, "Ivan Vecerina" <ivec@myrealbox.com>
>wrote:
>| >"Alf P. Steinbach" <alfps@start.no> wrote in message
>| >news:3f4226d5.414481453@News.CIS.DFN.DE...
>| >| The actual types are not needed at run-time, only the compatibility
>| >| matrix of catches and throws.
>| >|
>| >| That abstract matrix can be optimized in umpteen ways.
>| >|
>| >| One way, but not the only way, is to use RTTI.
>| >
>| >Actually, the exact same thing can be said of dynamic_cast:
>| >you only need a compatibility matrix of source and destination types.
>|
>| I see where the confusion stems from now.
>|
>| No, dynamic cast is a very different. With 'throw' the compiler knows
>| the exact type of the argument.
>
>Yes, just as when a new object is created (e.g. with the 'new' operator).
>
>| With 'dynamic_cast' the argument can
>| be a pointer to an object that according to the standard must have its
>| dynamic type checked (a pointer to 'throw' is not checked for dyntype).
>
>And when a catch clause is encountered during stack unwinding, the
>dynamic type of the exception being thrown (and eventually re-thrown)
>needs to be matched with the type of the catch clause.


At run-time there is no need for _type_ matching to determine the relevant
catch-clause, if any; only compatibility is relevant, and as mentioned the
compatibility matrix can be optimized and implemented in umpteen ways.

RTTI means "Run-Time Type Information", which isn't necessarily involved.


tom_usenet 08-20-2003 03:51 PM

Re: type_info, vtable
 
On Wed, 20 Aug 2003 14:50:01 GMT, alfps@start.no (Alf P. Steinbach)
wrote:

>>And when a catch clause is encountered during stack unwinding, the
>>dynamic type of the exception being thrown (and eventually re-thrown)
>>needs to be matched with the type of the catch clause.

>
>At run-time there is no need for _type_ matching to determine the relevant
>catch-clause, if any; only compatibility


Do you mean type compatibility? Or what?

is relevant, and as mentioned the
>compatibility matrix can be optimized and implemented in umpteen ways.


This is true for dynamic cast too - you only need to check the
compatibility of the source and destination types. In practice, may
dynamic_cast calls can be realized using a "compatibility matrix",
since the compiler can sometimes statically determine the type of the
object. Just as with exceptions, when it can *sometimes* statically
determine the call stack associated with a particular throw.

>RTTI means "Run-Time Type Information", which isn't necessarily involved.


Some processing has to be done at runtime that involves types. What do
you call it?

Tom


All times are GMT. The time now is 02:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.