简体   繁体   中英

How unsafe is unsafe code?

I'm reading about unsafe code, primarily for working with Bitmaps. I can't find, however, an explanation of the limits of unsafeness. I understand that using pointers on an array will not be checked so I might try accessing memory outside of its bounds. So far so good. But what if I do access that memory. What happens? Might I be changing other variables' values? Or even change the program's binary code? Or just raise an exception?

I'd like to know what's the worst that can happen before I decide to use unsafe code.

So unsafe code could lead to memory leaks or other random errors. Unsafe code is unsafe for a reason. For example in C++ you had pointers and those were basically pointing to chunks of memory on the physical machine. There are so many different things that could go wrong with a pointer that it is best not to use them if you aren't sure what you are doing. I think the same thing can be said of unsafe code in C#. itsme86 is right it is very unpredictable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM