纯C ++ / CLI的MSIL

时间:2011-10-28 19:58:16

标签: c++-cli cil

/clr:pure开关生成纯MSIL,但它不可验证。本机阵列和指针可用于此模式。这是否意味着MSIL中有一个结构来保存本机数组和指针?如果是,我想问一下如何编写MSIL本机数组和指针?

1 个答案:

答案 0 :(得分:4)

是的,CIL中有一个类型来表示非托管指针。它们类似于托管指针(C {中的refout,CIL中的&,除了GC忽略它们并且你可以对它们做一些算术运算(那些有意义的指针)。

有趣的是,指针类型确实包含有关目标类型的信息(例如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
}