纯 C++/CLI 的 MSIL

发布于 2024-12-12 05:41:37 字数 120 浏览 3 评论 0原文

/clr:pure 开关生成纯 MSIL,但不可验证。本机数组和指针可以在此模式下使用。这是否意味着 MSIL 中有一个结构来保存本机数组和指针?如果是的话,我想问一下如何编写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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-12-19 05:41:37

是的,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
}

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).

Interestingly, the pointer type does contain information about the target type (so it's e.g. int32*), but all arithmetic operations are byte based.

As an example, the following C++/CLI method:

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

produces the following CIL when it's inside a ref class (as reported by 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文