Is an object allowed to legally change its type during its lifetime in C++?

孤者浪人 提交于 2019-12-05 02:14:19

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

Consider:

object->first();

This call may destroy the object and create a new one in the same chunk of memory. Hence, after this call no assumptions can be made about the state. E.g.:

#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;
}

Compilers may optimize away unnecessary register loads if that function is inline and/or link-time code generation is used.


As DeadMG and Steve Jessop noted the above code exhibits undefined behaviour. According to 3.8/7 of C++ 2003 Standard:

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).

The above code doesn't satisfy requirement 2 from the above list.

It is stored in esi to be saved between the calls to different functions.

The Microsoft convention says

The compiler generates prolog and epilog code to save and restore the ESI, EDI, EBX, and EBP registers, if they are used in the function.

so the pointer stored in esi will remain, but the this pointer in ecx might not.

To answer the question from the title first:

Yes, an object from a derived class changes its type during construction and destruction. This is the only case.

The code in the body of the question is different. But as Maxim correctly notes, you just have a pointer. This pointer may point (at different times) to two different objects residing at the same address.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!