一千萬個為什麽

搜索

純C ++/CLI的MSIL

/clr:pure switch generates pure MSIL but it is not verifible. Native array and pointer can be used in this mode. Does that mean that there is a structure in MSIL to hold native arrays and pointers? If yes, I would like to ask how can I code MSIL native array and pointer?

最佳答案

Yes, there is a type in CIL to represent unmanaged pointers. They are similar to managed pointers (ref and out in C#, & in CIL), except that GC ignores them and you can do some arithmetic operations on them (those that make sense with pointers).

有趣的是,指針類型確實包含有關目標類型的信息(因此它是例如 int32 * ),但所有算術運算都是基於字節的。

例如,以下C ++/CLI方法:

void Bar(int *a)
{
    a[5] = 15;
}

當它在 ref class 中時產生以下CIL(由Reflector報告):

.method private hidebysig instance void Bar(int32* a) cil managed
{
    .maxstack 2
    L_0000: ldarg.1     //load the value of a pointer to the stack
    L_0001: ldc.i4.s 20 //load the number 20 (= 4 * 5) to the stack
    L_0003: add         //add 20 to the pointer
    L_0004: ldc.i4.s 15 //load the number 15 to the stack
    L_0006: stind.i4    //store the value of 15 at the computed address
    L_0007: ret         //return from the method
}

轉載註明原文: 純C ++/CLI的MSIL