24,855
社区成员
发帖
与我相关
我的任务
分享
struct _base1{};
struct _base2{};
struct _inheritance : _base1, _base2{};
int main()
{
// Ok no problem
_base1* p1 = new _inheritance;
delete p1;
// p2 now point to _base2 subobject, and the delete operator will cause error
_base2* p2 = new _inheritance;
delete p2;
return 0;
}
; this is the first case, p1 point to subobject _base1 which is just the address of _inheritance and no problem
00401000 push 1
00401002 call operator new (401048h)
00401007 push eax
00401008 call operator delete (40104Eh)
; this is the 2nd case, p2 point to subobject _base2 and ...
0040100D push 1
0040100F call operator new (401048h)
00401014 add esp,0Ch
00401017 test eax,eax
00401019 je wmain+2Ah (40102Ah)
0040101B add eax,1 ; here, for multi inheritance, the point add an offset of 1 to subobject _base2
0040101E push eax
0040101F call operator delete (40104Eh) ; and, the call to operator delete will cause error, the free failed.
struct _inheritance : _base1, _base2, _base3 {};
_base3* p3 = new _inheritance;
delete p3;