非非托管类型和托管类型之间有什么区别?

时间:2022-09-01 11:36:03

When I wrote the following snippet for experimenting purposes, it raised the hover-error (see screenshot):

当我为实验目的编写以下代码段时,它引发了悬停错误(参见屏幕截图):

Cannot declare pointer to non-unmanaged type 'dynamic'

无法声明指向非非托管类型的'动态'

The snippet:

dynamic* pointerToDynamic = &fields;

While the code is clearly not allowed (you cannot take the address of a managed type), it raised with me the question: what is a non-unmanaged type and how is it different to a managed type? Or is it just Visual Studio trying to be funny?

虽然代码显然不被允许(你不能获取托管类型的地址),但它提出了一个问题:什么是非非托管类型,它与托管类型有什么不同?或者只是Visual Studio试图变得有趣?

非非托管类型和托管类型之间有什么区别?

2 个解决方案

#1


6  

There is a difference between unmanaged and non-managed pointers.

非托管指针和非托管指针之间存在差异。

A managed pointer is a handle to an object on the managed heap, and AFAIK is available in managed C++ only. It is equivalent of C# reference to an object. Unmanaged pointer, on the other hand, is equivalent of a traditional C-style pointer, i.e. address of a memory location; C# provides unary & operator, fixed keyword and unsafe context for that.

托管指针是托管堆上对象的句柄,AFAIK仅在托管C ++中可用。它等同于对象的C#引用。另一方面,非托管指针等同于传统的C风格指针,即存储器位置的地址; C#为此提供一元和运算符,固定关键字和不安全上下文。

You are trying to get a pointer to a managed field (dynamic is actually System.Object is disguise), while C# allows pointers to unmanaged objects only, hence the wording: your type is non-unmanaged.

您正在尝试获取指向托管字段的指针(动态实际上是System.Object伪装),而C#仅允许指向非托管对象的指针,因此措辞:您的类型是非托管的。

A bit more on this here.

在这里再说一点。

Update: to make it more clear, managed C++ supports classic C-style pointers and references. But to keep C++ terminology consistent, they are called unmanaged and managed pointers, correspondingly. C# also supports pointers (explicitly in unsafe context) and references (implicitly whenever objects of reference types are involved), but the latter is not called "managed pointers", they are just references.

更新:为了使其更清晰,托管C ++支持经典的C风格指针和引用。但是为了保持C ++术语的一致性,相应地将它们称为非托管和托管指针。 C#还支持指针(显式地在不安全的上下文中)和引用(隐含地涉及引用类型的对象),但后者不称为“托管指针”,它们只是引用。

To sum up: in C++ there are unmanaged and managed pointers, in C# - unmanaged pointers and references.

总结一下:在C ++中有非托管和托管指针,在C#中是非托管指针和引用。

Hope it makes sense now.

希望现在有意义。

#2


2  

You cannot create a pointer to a managed type. While int, double, etc are managed, they have unmanaged counterparts.

您无法创建指向托管类型的指针。虽然管理int,double等,但它们具有非托管对应物。

So what non-unmanaged type really means is the managed type.

那么非非托管类型真正意味着托管类型。

The problem here is that the managed type since is sitting on the heap, you cannot get a pointer to. You can get a pointer using fixed keyword but that is mainly for arrays.

这里的问题是托管类型因为坐在堆上,你无法获得指针。您可以使用固定关键字获取指针,但主要用于数组。

#1


6  

There is a difference between unmanaged and non-managed pointers.

非托管指针和非托管指针之间存在差异。

A managed pointer is a handle to an object on the managed heap, and AFAIK is available in managed C++ only. It is equivalent of C# reference to an object. Unmanaged pointer, on the other hand, is equivalent of a traditional C-style pointer, i.e. address of a memory location; C# provides unary & operator, fixed keyword and unsafe context for that.

托管指针是托管堆上对象的句柄,AFAIK仅在托管C ++中可用。它等同于对象的C#引用。另一方面,非托管指针等同于传统的C风格指针,即存储器位置的地址; C#为此提供一元和运算符,固定关键字和不安全上下文。

You are trying to get a pointer to a managed field (dynamic is actually System.Object is disguise), while C# allows pointers to unmanaged objects only, hence the wording: your type is non-unmanaged.

您正在尝试获取指向托管字段的指针(动态实际上是System.Object伪装),而C#仅允许指向非托管对象的指针,因此措辞:您的类型是非托管的。

A bit more on this here.

在这里再说一点。

Update: to make it more clear, managed C++ supports classic C-style pointers and references. But to keep C++ terminology consistent, they are called unmanaged and managed pointers, correspondingly. C# also supports pointers (explicitly in unsafe context) and references (implicitly whenever objects of reference types are involved), but the latter is not called "managed pointers", they are just references.

更新:为了使其更清晰,托管C ++支持经典的C风格指针和引用。但是为了保持C ++术语的一致性,相应地将它们称为非托管和托管指针。 C#还支持指针(显式地在不安全的上下文中)和引用(隐含地涉及引用类型的对象),但后者不称为“托管指针”,它们只是引用。

To sum up: in C++ there are unmanaged and managed pointers, in C# - unmanaged pointers and references.

总结一下:在C ++中有非托管和托管指针,在C#中是非托管指针和引用。

Hope it makes sense now.

希望现在有意义。

#2


2  

You cannot create a pointer to a managed type. While int, double, etc are managed, they have unmanaged counterparts.

您无法创建指向托管类型的指针。虽然管理int,double等,但它们具有非托管对应物。

So what non-unmanaged type really means is the managed type.

那么非非托管类型真正意味着托管类型。

The problem here is that the managed type since is sitting on the heap, you cannot get a pointer to. You can get a pointer using fixed keyword but that is mainly for arrays.

这里的问题是托管类型因为坐在堆上,你无法获得指针。您可以使用固定关键字获取指针,但主要用于数组。