繁体   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