簡體   English   中英

這個電話從哪里來?

[英]Where does this call come from?

我有一些代碼大致類似於以下代碼:

class C {
    string s;
    static C a = new C();

    static void Main() {
        C b = a;
        b.s = "hello";
}

在釋放模式下, Main方法的反匯編如下:

        C b = a;
00000000  push        ebp 
00000001  mov         ebp,esp 
00000003  push        eax 
00000004  cmp         dword ptr ds:[04581D9Ch],0 
0000000b  je          00000012 
0000000d  call        763B3BC3 
00000012  xor         edx,edx 
00000014  mov         dword ptr [ebp-4],edx 
00000017  mov         eax,dword ptr ds:[01B24E20h] ; Everything up to this point
0000001c  mov         dword ptr [ebp-4],eax        ; is fairly clear.
        b.s = "hello";
0000001f  mov         eax,dword ptr ds:[01B22088h] ; Loads the address of "hello"
00000025  mov         ecx,dword ptr [ebp-4]        ; Loads the address of b
00000028  lea         edx,[ecx+4]                  ; Loads the address of (the reference to?) b.s
0000002b  call        76100AE0                     ; ??
    }
00000030  nop 
00000031  mov         esp,ebp 
00000033  pop         ebp 
00000034  ret 

我不明白為什么要在nb打電話。 看來bss的地址已作為參數傳遞,但是由於這是一個簡單的指針分配,因此為什么有必要?

(這種行為似乎發生在許多指針分配中。但是,分配null似乎並不遵循這種模式。)

猜猜:它正在設置GC卡表,因為您正在創建一個從堆字段到堆對象的新引用。

你說:“這種現象似乎發生了很多分配的指針”。 這完全符合該解釋。

如果string的例子是std::string ,那么你無辜的前瞻性分配如下所示:

mov  eax, offset "hello"
mov  ecx, b
lea  edx, [ecx+4] ; edx = &b.s
call std::string::operator=(const char *)

(似乎此特定編譯期望edx “ this”和eax參數-可能是整個程序優化的結果-傳統約定是ecx中的this和堆棧上的其他參數。)

C ++和STL為您提供了不錯的,幾乎毫不費力的內存管理。 但是它不是免費的。 無論它在源代碼中如何顯示,它絕對不是“簡單的指針分配”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM