一个对象允许在C的生命周期内合法改变其类型吗?

我有这个代码
class Class {
public:
    virtual void first() {};
    virtual void second() {};
};

Class* object = new Class();
object->first();
object->second();
delete object;

我用Visual C 10与/ O2编译,并有这个反汇编:

282:    Class* object = new Class();
00403953  push        4  
00403955  call        dword ptr [__imp_operator new (4050BCh)]  
0040395B  add         esp,4  
0040395E  test        eax,eax  
00403960  je          wmain+1Ch (40396Ch)  
00403962  mov         dword ptr [eax],offset Class::`vftable' (4056A4h)  
00403968  mov         esi,eax  
0040396A  jmp         wmain+1Eh (40396Eh)  
0040396C  xor         esi,esi  
283:    object->first();
0040396E  mov         eax,dword ptr [esi]  
00403970  mov         edx,dword ptr [eax]  
00403972  mov         ecx,esi  
00403974  call        edx  
284:    object->second();
00403976  mov         eax,dword ptr [esi]  
00403978  mov         edx,dword ptr [eax+4]  
0040397B  mov         ecx,esi  
0040397D  call        edx  
285:    delete object;
0040397F  push        esi  
00403980  call        dword ptr [__imp_operator delete (405138h)]

请注意,在00403968,将对象start(其中存储vptr的地址)复制到esi寄存器中.然后在0040396E,该地址用于检索vptr,并使用vptr值来检索first()的地址.然后在00403976再次检索vptr,并用于检索second()的地址.

为什么vptr检索两次?可能的对象可能会在调用之间改变其vptr,还是只是一个低优化?

解决方法

Why is vptr retrieved twice? Could the object possible have its vptr changed in between calls or is it just an underoptimization?

考虑:

object->first();

调用可能会破坏对象并在同一内存块中创建一个新的对象.因此,在这个呼吁之后,不能对国家作出任何假设.例如.:

#include <new>

struct Class {
    virtual void first();
    virtual void second() {}
    virtual ~Class() {}
};

struct OtherClass : Class {
    void first() {}
    void second() {}
};

void Class::first() {
    void* p = this;
    static_assert(sizeof(Class) == sizeof(OtherClass),"Oops");
    this->~Class();
    new (p) OtherClass;
}

int main() {
    Class* object = new Class();
    object->first();
    object->second();
    delete object;
}

如果使用内联函数和/或链接代码生成,编译器可以优化不必要的寄存器负载.

正如DeadMG和Steve Jessop所说,上述代码展现了未定义的行为.根据C 2003标准的3.8 / 7:

If,after the lifetime of an object has ended and before the storage which the object occupied is reused or released,a new object is created at the storage location which the original object occupied,a pointer that pointed to the original object,a reference that referred to the original object,or the name of the original object will automatically refer to the new object and,once the lifetime of the new object has started,can be used to manipulate the new object,if:

  • the storage for the new object exactly overlays the storage location which the original object occupied,and
  • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers),and
  • the type of the original object is not const-qualified,and,if a class type,does not contain any non-static data member whose type is const-qualified or a reference type,and
  • the original object was a most derived object (1.8) of type T and the new object is a most derived object of type T (that is,they are not base class subobjects).

上述代码不符合上述列表中的要求2.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...