何时在 C#/.NET 中使用指针? [英] When to use pointers in C#/.NET?

查看:40
本文介绍了何时在 C#/.NET 中使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 C# 使程序员能够在不安全的上下文中访问和使用指针.但是什么时候需要这个?

I know C# gives the programmer the ability to access, use pointers in an unsafe context. But When is this needed?

在什么情况下,使用指针变得不可避免?

At what circumstances, using pointers becomes inevitable?

仅仅是出于性能原因吗?

Is it only for performance reasons?

此外,为什么 C# 会通过不安全的上下文公开此功能,并从中删除所有托管优势?理论上是否可以在不失去托管环境的任何优势的情况下使用指针?

Also why does C# expose this functionality through an unsafe context, and remove all of the managed advantages from it? Is it possible to have use pointers without losing any advantages of managed environment, theoretically?

推荐答案

什么时候需要?在什么情况下使用指针变得不可避免?

When is this needed? Under what circumstances does using pointers becomes inevitable?

当托管的安全解决方案的净成本不可接受但不安全解决方案的净成本可以接受时.您可以通过从总成本中减去总收益来确定净成本或净收益.不安全解决方案的好处是没有时间浪费在不必要的运行时检查上以确保正确性";成本是 (1) 即使在托管安全系统关闭的情况下也必须编写安全的代码,以及 (2) 必须处理可能使垃圾收集器效率降低的问题,因为它无法在具有非托管指针的内存中移动

When the net cost of a managed, safe solution is unacceptable but the net cost of an unsafe solution is acceptable. You can determine the net cost or net benefit by subtracting the total benefits from the total costs. The benefits of an unsafe solution are things like "no time wasted on unnecessary runtime checks to ensure correctness"; the costs are (1) having to write code that is safe even with the managed safety system turned off, and (2) having to deal with potentially making the garbage collector less efficient, because it cannot move around memory that has an unmanaged pointer into it.

或者,如果您是编写编组层的人.

Or, if you are the person writing the marshalling layer.

仅仅是出于性能原因吗?

Is it only for performance reasons?

出于性能以外的原因在托管语言中使用指针似乎有悖常理.

It seems perverse to use pointers in a managed language for reasons other than performance.

在绝大多数情况下,您可以使用 Marshal 类中的方法来处理与非托管代码的互操作.(在某些情况下,使用编组工具很难或不可能解决互操作问题,但我不知道.)

You can use the methods in the Marshal class to deal with interoperating with unmanaged code in the vast majority of cases. (There might be a few cases in which it is difficult or impossible to use the marshalling gear to solve an interop problem, but I don't know of any.)

当然,正如我所说,如果您是编写 Marshal 类的人,那么显然您无法使用编组层来解决您的问题.在这种情况下,您需要使用指针来实现它.

Of course, as I said, if you are the person writing the Marshal class then obviously you don't get to use the marshalling layer to solve your problem. In that case you'd need to implement it using pointers.

为什么 C# 会通过不安全的上下文公开此功能,并从中删除所有托管优势?

Why does C# expose this functionality through an unsafe context, and remove all of the managed advantages from it?

这些管理优势伴随着性能成本.例如,每次向数组询问其第十个元素时,运行时都需要检查是否有第十个元素,如果没有则抛出异常.使用消除运行时成本的指针.

Those managed advantages come with performance costs. For example, every time you ask an array for its tenth element, the runtime needs to do a check to see if there is a tenth element, and throw an exception if there isn't. With pointers that runtime cost is eliminated.

相应的开发人员成本是,如果您做错了,那么您将需要处理内存损坏错误,这些错误会格式化您的硬盘并在一小时后使您的进程崩溃,而不是在错误点处理一个干净的异常.

The corresponding developer cost is that if you do it wrong then you get to deal with memory corruption bugs that formats your hard disk and crashes your process an hour later rather than dealing with a nice clean exception at the point of the error.

理论上是否可以在不失去托管环境优势的情况下使用指针?

Is it possible to use pointers without losing any advantages of managed environment, theoretically?

我认为优势"是指垃圾收集、类型安全和参照完整性等优势.因此,您的问题本质上是理论上是否可以关闭安全系统,但仍然可以从安全系统开启的情况下获得好处?"不,显然不是.如果您因为不喜欢它的价格而关闭该安全系统,那么您就无法享受它带来的好处!

By "advantages" I assume you mean advantages like garbage collection, type safety and referential integrity. Thus your question is essentially "is it in theory possible to turn off the safety system but still get the benefits of the safety system being turned on?" No, clearly it is not. If you turn off that safety system because you don't like how expensive it is then you don't get the benefits of it being on!

这篇关于何时在 C#/.NET 中使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆