Safe vs Unsafe code

我只是一个虾纸丫 提交于 2019-12-18 21:58:43

问题


Read this question today about safe and unsafe code I then read about it in MSDN but I still don't understand it. Why would you want to use pointers in C#? Is this purely for speed?


回答1:


There are three reasons to use unsafe code:

  • APIs (as noted by John)
  • Getting actual memory address of data (e.g. access memory-mapped hardware)
  • Most efficient way to access and modify data (time-critical performance requirements)



回答2:


Sometimes you'll need pointers to interface your C# to the underlying operating system or other native code. You're strongly discouraged from doing so, as it is "unsafe" (natch).

There will be some very rare occasions where your performance is so CPU-bound that you need that minuscule extra bit of performance. My recommendation would be to write those CPU-intesive pieces in a separate module in assembler or C/C++, export an API, and have your .NET code call that API. An possible additional benefit is that you can put platform-specific code in the unmanaged module, and leave the .NET platform agnostic.




回答3:


I tend to avoid it, but there are some times when it is very helpful:

  • for performance working with raw buffers (graphics, etc)
  • needed for some unmanaged APIs (also pretty rare for me)
  • for cheating with data

For example of the last, I maintain some serialization code. Writing a float to a stream without having to use BitConverter.GetBytes (which creates an array each time) is painful - but I can cheat:

float f = ...;
int i = *(int*)&f;

Now I can use shift (>>) etc to write i much more easily than writing f would be (the bytes will be identical to if I had called BitConverter.GetBytes, plus I now control the endianness by how I choose to use shift).




回答4:


There is at least one managed .Net API that often makes using pointers unavoidable. See SecureString and Marshal.SecureStringToGlobalAllocUnicode.

The only way to get the plain text value of a SecureString is to use one of the Marshal methods to copy it to unmanaged memory.



来源:https://stackoverflow.com/questions/2760924/safe-vs-unsafe-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!