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

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

问题描述

我知道C#为程序员提供了在不安全的情况下能够接通,使用指针。但是,当这种需要?

在什么情况下,使用指针成为必然?

难道仅仅是出于性能的考虑?

另外为什么C#通过一个不安全的情况下公开此功能,并拆除所有的管理优势?是否有可能有使用指针,而不会失去管理环境的任何优势,从理论上说?

解决方案
  

当这个需要?在什么情况下不使用指针成为必然?

在有管理的,安全的解决方案的净成本是不能接受的,但一个不安全的解决方案的净成本是可以接受的。您可以确定通过减去总成本总收益的净成本或净效益。一个不安全的解决方案的好处是像没有时间浪费在不必要的运行时检查,以确保正确;的成本是:(1),其具有写入code表示是安全的,即使与被管理的安全系统关闭,并且(2)具有以处理可能使垃圾收集器的效率较低,因为它不能左右存储器,其具有移动非托管指针进去。

或者,如果你正在写的编组层的人。

  

难道仅仅是出于性能的考虑?

这似乎有悖常理的使用指针的原因而非性能以外的其他托管语言。

您可以使用Marshal类的方法来处理与非托管code案件的绝大多数互操作。 (可能有一些案件中,这是很难或根本无法使用的编组齿轮解决的互操作性问题,但我不知道有什么。)

当然,正如我所说,如果你正在写Marshal类的人那么显然你没有得到的使用的编组层,解决你的问题。在这种情况下,你需要使用指针来实现它。

  

为什么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?

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?

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.

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

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.

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天全站免登陆