研究了一波RTTI,你会了吗

开发 前端
RTTI 是 Run Time Type Information 的缩写,从字面上来理解就是运行时期的类型信息,它的主要作用就是动态判断运行时期的类型。

最近研究了一波RTTI,整理了一下知识点,在这里分享一下,下面是目录:

RTTI 是 Run Time Type Information 的缩写,从字面上来理解就是运行时期的类型信息,它的主要作用就是动态判断运行时期的类型。

一般在dynamic_cast和typeid中用到,例如父类B的指针转换子类A的指针,dynamic_cast会判断B究竟是不是A的父类,如果不是,会返回nullptr,相对于强转会更加安全。依据什么判断的呢?就是RTTI。

先看下面这段代码:

  1. #include <iostream> 
  2. using std::cout; 
  3. using std::endl; 
  4. class Base 
  5. public
  6.     int a; 
  7.     int b; 
  8.     Base() 
  9.     { 
  10.         cout << this << " Base \n"
  11.     } 
  12.     virtual void func() 
  13.         cout << this << " hello Base \n"
  14.     }; 
  15.     void basefunc() 
  16.         cout << this << " hello basefunc \n"
  17.     } 
  18. }; 
  19. class BaseBB 
  20. public
  21.     int d; 
  22.     int c; 
  23.     BaseBB() 
  24.     { 
  25.         cout << this << " BaseBB \n"
  26.     } 
  27.     virtual void func() 
  28.         cout << this << " hello BaseBB \n"
  29.     } 
  30. }; 
  31. class Derive : public Base 
  32. public
  33.     Derive() 
  34.     { 
  35.         cout << this << " Derive \n"
  36.     } 
  37.     void func() override 
  38.         cout << this << " hello Derive \n"
  39.     } 
  40. }; 
  41. int main() 
  42.     Derive *d = new Derive; 
  43.     typeid(d); 
  44.     d->func(); 
  45.     Base *b = static_cast<Base *>(d); 
  46.     b->func(); 
  47.     b->basefunc(); 
  48.     Derive *b1 = dynamic_cast<Derive *>(b); 
  49.     Derive *b2 = static_cast<Derive *>(b); 
  50.     b1->func(); 
  51.     b2->func(); 
  52.     BaseBB *b3 = dynamic_cast<BaseBB *>(b); 
  53.     BaseBB *b4 = reinterpret_cast<BaseBB *>(b); 
  54.     cout << d << " " << b << " " << b1 << " " << b2 << " " << b3 << " " << b4 << endl; 
  55.     return 0; 

结果如下:

  1. clang++ test_rtti.cc -std=c++11;./a.out 
  2.  
  3. 0x7fe80ac05920 Base  
  4. 0x7fe80ac05920 Derive  
  5. 0x7fe80ac05920 hello Derive  
  6. 0x7fe80ac05920 hello Derive  
  7. 0x7fe80ac05920 hello basefunc  
  8. 0x7fe80ac05920 hello Derive  
  9. 0x7fe80ac05920 hello Derive  
  10. 0x7fe80ac05920 0x7fe80ac05920 0x7fe80ac05920 0x7fe80ac05920 0x0 0x7fe80ac05920 

上面的代码是正常的一段使用多态的代码,同时也包含了子类指针转基类指针,基类指针转子类指针,从输出结果中可以看到,使用dynamic_cast进行不合理的基类子类指针转换时,会返回nullptr,而强转则不会返回nullptr,运行时肯定就会出现奇奇怪怪的错误,比较难排查。

如果在编译时加上-fno-rtti会怎么样?结果是这样:

  1. clang++ test_rtti.cc -std=c++11 -fno-rtti 
  2.  
  3. test_rtti.cc:60:5: error: use of typeid requires -frtti 
  4.     typeid(d); 
  5.     ^ 
  6. test_rtti.cc:65:18: error: use of dynamic_cast requires -frtti 
  7.     Derive *b1 = dynamic_cast<Derive *>(b); 
  8.                  ^ 
  9. test_rtti.cc:69:18: error: use of dynamic_cast requires -frtti 
  10.     BaseBB *b3 = dynamic_cast<BaseBB *>(b); 
  11.                  ^ 
  12. 3 errors generated. 

可以看到,加上了-fno-rtti编译时,使用typeid或dynamic_cast会报错,即添加-fno-rtti编译会禁止我们使用dynamic_cast和typeid。那为什么要禁止使用他们呢?

1. RTTI的空间成本非常高:每个带有vtable(至少一个虚拟方法)的类都将获得RTTI信息,其中包括类的名称及其基类的信息。此信息用于实现typeid运算符以及dynamic_cast。(大小问题大家可以自己编写代码验证一下)

2. 速度慢,运行时多判断了一层,性能肯定更慢一些。

tips:我这里又将typeid和dynamic_cast去掉重新编译,结果表明添加了-fno-rtti,还是可以正常使用多态,所以大家不用担心rtti的禁用会影响多态的使用。

都知道RTTI信息是存在于虚函数表中,而添加-fno-rtti后代表禁止了RTTI,那虚函数表中还会有rtti信息吗?

我这里使用clang的命令查看一下虚函数表:

  1. clang -Xclang -fdump-vtable-layouts -stdlib=libc++ -fno-rtti -c test_rtti.cc 
  2.  
  3. test_rtti.cc:51:17: warning: 'override' keyword is a C++11 extension [-Wc++11-extensions] 
  4.     void func() override 
  5.                 ^ 
  6. Original map 
  7.  void Derive::func() -> void Base::func() 
  8. Vtable for 'Derive' (3 entries). 
  9.    0 | offset_to_top (0) 
  10.    1 | Derive RTTI 
  11.        -- (Base, 0) vtable address -- 
  12.        -- (Derive, 0) vtable address -- 
  13.    2 | void Derive::func() 
  14.  
  15. VTable indices for 'Derive' (1 entries). 
  16.    0 | void Derive::func() 

通过结果可以看到,即使添加了-fno-rtti,虚函数表中还是会存在RTTI指针,但是我查看很多文档都说rtti会导致可执行文件的体积增大一些(毕竟-fno-rtti最大的目的就是为了减小代码和可执行文件的大小),所以我估计指针指向的块里面可能什么信息都没有,具体就不得而知了。

 

责任编辑:武晓燕 来源: 程序喵大人
相关推荐

2022-12-07 10:17:32

WindowsANSI

2022-04-19 08:28:34

main函数

2021-11-08 12:44:48

AndroidC++内存

2021-01-01 09:03:44

故障HAProxy服务器

2014-07-17 09:05:29

2024-01-19 08:25:38

死锁Java通信

2022-04-01 08:23:17

InputstreString字符串

2024-02-04 00:00:00

Effect数据组件

2023-07-26 13:11:21

ChatGPT平台工具

2023-01-10 08:43:15

定义DDD架构

2023-03-10 22:08:20

2024-01-02 12:05:26

Java并发编程

2023-08-01 12:51:18

WebGPT机器学习模型

2020-08-06 17:16:47

抖音Tiktok美国

2021-07-01 19:22:33

脚本Shell参数

2021-09-01 13:46:07

GitHub Copi漏洞代码训练

2023-01-30 09:01:54

图表指南图形化

2022-05-13 08:17:05

HTTPRESTful架构

2022-07-08 09:27:48

CSSIFC模型

2023-10-10 11:04:11

Rust难点内存
点赞
收藏

51CTO技术栈公众号