C#有一个ref关键字.使用ref,您可以通过引用将int传递给方法.当您调用通过引用接受int的方法时,堆栈帧上会发生什么?
public void SampleMethod(ref int i) { }
Run Code Online (Sandbox Code Playgroud)
Tam*_*dus 18
在低级别,引用的局部int变量将被放在堆栈上(大多数时候整数存储在寄存器中),并且指向堆栈的指针将被传递给调用的函数(指针本身最有可能被传入登记册).请考虑以下示例:
var i = 7;
Console.WriteLine(i);
inc(ref i);
Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud)
这将是JIT-et之类的东西(目标架构是x86):
17: var i = 7;
# allocate space on the stack for args and i
00482E3B sub esp,8
# initialize i to 0
00482E3E xor eax,eax
00482E40 mov dword ptr [ebp-8],eax
# args saved to stack (could be optimised out)
00482E43 mov dword ptr [ebp-4],ecx
00482E46 cmp dword ptr ds:[3ACAECh],0
00482E4D je 00482E54
00482E4F call 7399CB2D
# i = 7
00482E54 mov dword ptr [ebp-8],7
18: Console.WriteLine(i);
# load the value of i into ecx, and call cw
00482E5B mov ecx,dword ptr [ebp-8]
00482E5E call 72E729DC
19: inc(ref i);
# load the address of i into ecx, and call inc
00482E63 lea ecx,[ebp-8]
00482E66 call dword ptr ds:[4920860h]
20: Console.WriteLine(i);
# load the value of i into ecx, and call cw
00482E6C mov ecx,dword ptr [ebp-8]
00482E6F call 72E729DC
21: }
00482E74 nop
00482E75 mov esp,ebp
00482E77 pop ebp
00482E78 ret
Run Code Online (Sandbox Code Playgroud)
这里发生了同样的事情,获得了字段或元素的地址,并将指针传递给函数:
var i = new[]{7};
Console.WriteLine(i[0]);
inc(ref i[0]);
Console.WriteLine(i[0]);
Run Code Online (Sandbox Code Playgroud)
编译成(没有无聊的部分):
18: Console.WriteLine(i[0]);
00C82E91 mov eax,dword ptr [ebp-8]
00C82E94 cmp dword ptr [eax+4],0
00C82E98 ja 00C82E9F
00C82E9A call 7399BDC2
00C82E9F mov ecx,dword ptr [eax+8]
00C82EA2 call 72E729DC
19: inc(ref i[0]);
# loading the reference of the array to eax
00C82EA7 mov eax,dword ptr [ebp-8]
# array boundary check is inlined
00C82EAA cmp dword ptr [eax+4],0
00C82EAE ja 00C82EB5
# this would throw an OutOfBoundsException, but skipped by ja
00C82EB0 call 7399BDC2
# load the address of the element in ecx, and call inc
00C82EB5 lea ecx,[eax+8]
00C82EB8 call dword ptr ds:[4F80860h]
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下不必固定数组,因为框架知道地址ecx是指向数组内的项目,因此如果在inc函数之间lea和之call内发生堆压缩,它可以重新调整值的ecx直接.
您可以通过打开Disassembly窗口(Debug/Windows/Disassembly)使用Visual Studio调试器自行调查JIT-ed程序集
下面是一个简单的 C# 代码示例:
void Main()
{
int i = 1;
inc(ref i);
Console.WriteLine(i);
}
public void inc(ref int i) {
i++;
}
Run Code Online (Sandbox Code Playgroud)
这是生成的IL代码
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0 // i
IL_0003: ldarg.0
IL_0004: ldloca.s 00 // i
IL_0006: call inc
IL_000B: nop
IL_000C: ldloc.0 // i
IL_000D: call System.Console.WriteLine
IL_0012: nop
IL_0013: ret
inc:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: dup
IL_0003: ldind.i4
IL_0004: ldc.i4.1
IL_0005: add
IL_0006: stind.i4
IL_0007: ret
Run Code Online (Sandbox Code Playgroud)
请注意,对于这个简单的情况,实际上只有一个区别:ldloca.s 00 或 ldloc.0。加载本地或加载地址(偏移量 00)
这是最简单级别的差异(这就是您在评论中所要求的)——如果您加载变量的值或加载变量的地址。事情很快就会变得复杂——如果您调用的函数不是本地的,如果您传递的变量不是本地的等等。但在基本层面上,这就是区别。
我使用 linqpad 进行快速 diss 汇编——我推荐它。 http://www.linqpad.net/
| 归档时间: |
|
| 查看次数: |
858 次 |
| 最近记录: |