简体   繁体   English

C ++虚拟函数和可执行文件

[英]c++ virtual function and executable

There are a few questions concerning vptr and exe: 关于vptr和exe有几个问题:

1) When is vTable created and populated ie compile-time or run-time? 1)什么时候创建和填充vTable,即编译时或运行时?

2) Does compiler put vptr in exe ie size of exe increases with no. 2)编译器是否将vptr放入exe中,即exe的大小没有增加。 of classes having virtual functions 具有虚拟功能的类

3) Does executable size grows when we run it and when it contain virtual functions 3)当我们运行可执行文件并包含虚拟函数时,可执行文件的大小是否会增加

This is completey compiler-dependent. 这完全取决于编译器。 The C++ standard has no notion of a vtable. C ++标准没有vtable的概念。

This is all implementation defined and will vary from compiler to compiler, The standard does not mention how dynamic dispatch should be implemented, it does not even use the words vtable and vpointer, but all known compilers implement dynamic dispatch through vtable and assuming that answers to your questions are: 这是所有实现的定义,并且会因编译器的不同而有所不同。该标准没有提及应如何实现动态调度,甚至没有使用vtable和vpointer一词,但是所有已知的编译器都通过vtable实现动态调度,并假定对您的问题是:

When is vTable created and populated ie compile-time or run-time? vTable何时创建和填充(即编译时或运行时)?
Compile time 编译时间
vtable is created for each class with atleast one virtual method during compilation phase. 在编译阶段,将使用至少一个虚拟方法为每个类创建vtable。

Does compiler put vptr in exe ie size of exe increases with no. 编译器是否将vptr放入exe中,即exe的大小没有增加。 of classes having virtual functions? 具有虚拟功能的类?
Yes, most likely 是的,很可能
Since the vtable has to reside somewhere in memory, it will occupy some memory space for sure. 由于vtable必须驻留在内存中的某个位置,因此肯定会占用一些内存空间。

Does executable size grows when we run it and when it contain virtual functions? 当我们运行可执行文件并包含虚拟函数时,可执行文件的大小会增加吗?
No there is no runtime growth of exe. 没有,exe没有运行时增长。
Only the dispatch of functions happens at run-time, the mechanism to make that dispatch happen is constructed at compilation. 只有函数的分派在运行时发生,使该分派发生的机制是在编译时构造的。

The actual implementation isn't defined by the standard, but typically, there will usually be only one vtable per class, with static lifetime. 该标准并未定义实际的实现,但是通常,每个类通常只有一个vtable,并且具有静态生存期。 This will increase the size of the executable, but normally only marginally—nothing significant. 这将增加可执行文件的大小,但通常只会增加一点点,没有什么意义。

The vptr is part of the class image: it will increase the size of a class instance by the size of a pointer (or more, depending on alignment restrictions and where the compiler puts it in the class). vptr是类映像的一部分:它将通过指针的大小(或更多,取决于对齐限制以及编译器将其放入类的位置)来增加类实例的大小。 It will also cause additional code to be generated for the constructor; 这还将导致为构造函数生成其他代码。 again, this is almost always neglible. 同样,这几乎总是疏忽大意。 The fact that often a virtual function cannot be inlined can have a significant impact on performance, and in some cases, size as well; 通常无法内联虚拟功能的事实可能会对性能产生重大影响,在某些情况下还对大小产生重大影响; inlining a simple function can often expose additional possibilities for optimization. 内联简单函数通常可以提供优化的其他可能性。

Usually, for gcc, the VTable is created at compile-time, although it may be adjusted at runtime if you have -fPIC . 通常,对于gcc,VTable是在编译时创建的,但是如果您具有-fPIC ,则可以在运行时对其进行调整。 It costs a few bytes per class, per function: a negligible amount compared to everything else. 每个类,每个函数花费几个字节:与其他所有东西相比,这个数量可以忽略不计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM