I have this code:
class Class {
public:
virtual void first() {};
virtual void second() {};
};
Class* object = new Class();
object->first();
object->second();
delete object;
that I compile with Visual C++ 10 with /O2 and have this disassembly:
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)]
Note that at 00403968
the address of the object start (where vptr
is stored) is copied into esi
register. Then at 0040396E
this address is used to retrieve the vptr
and the vptr
value is used to retrieve address of first()
. Then at 00403976
the vptr
is retrieved again and is used to retrieve the address of second()
.
Why is vptr retrieved twice? Could the object possible have its vptr
changed in between calls or is it just an underoptimization?
esi
. – Drumlin