release vs 在彙編中堆物件如何確定建構函式的範圍
阿新 • • 發佈:2022-04-11
release vs 在彙編中堆物件如何確定建構函式的範圍
#include <stdio.h> class Person { public: Person() { age = 20; } virtual int getAge(){ return this->age; } int age; }; int main(int argc, char* argv[]) { Person *p = new Person; //為了突出本節討論的問題,這裡沒有檢查new運算的返回值 printf("%d\n", p->age); return 0; }
彙編
.text:0000000140001020 sub rsp, 28h .text:0000000140001024 mov ecx, 10h ; Size .text:0000000140001029 call ??2@YAPEAX_K@Z ; operator new(unsigned __int64) .text:000000014000102E test rax, rax .text:0000000140001031 jz short IF_END_140001044 .text:0000000140001033 lea rcx, ??_7Person@@6B@ ; const Person::`vftable' .text:000000014000103A mov dword ptr [rax+8], 20 .text:0000000140001041 mov [rax], rcx .text:0000000140001044 .text:0000000140001044 IF_END_140001044: ; CODE XREF: main+11↑j .text:0000000140001044 mov edx, [rax+8] .text:0000000140001047 lea rcx, aD ; "%d\n" .text:000000014000104E call _printf .text:0000000140001053 xor eax, eax .text:0000000140001055 add rsp, 28h .text:0000000140001059 retn .text:0000000140001059 main endp
在上面看到在new了記憶體後有一個單分支結構,判斷記憶體是否申請成功,以此來決定要不要執行建構函式
找到new運算後,可立即在下文中尋找判定new返回值的程式碼,在判定成功(new的返回值非0)的分支迅速定位並得到建構函式的範圍