|
-
Nov 12th, 2007, 01:43 PM
#1
[2.0] "Unsafe" code?
A little behind the point, probably, as I'm already using it.. but I'm still curious as to exactly what it is and why it's unsafe. As it is right now, I'm using unsafe code for drawing pixels. Pointer uints and the like. I know I have to declare it an unsafe class and compile it as unsafe for it to operate correctly, but why is it unsafe?
My best guess tells me that its unsafe because I'd be bypassing the OS for memory management and using the pointers to directly access blocks of memory. Is this close?
-
Nov 12th, 2007, 02:44 PM
#2
-
Nov 12th, 2007, 05:52 PM
#3
Re: [2.0] "Unsafe" code?
To clarify (possibly needlessly), using managed references is safe because the Framework will never let you access memory you shouldn't. Using pointers you are, as you say, accessing memory directly so it is not inherently safe. It's left up to you to ensure that you don't overwrite memory that you shouldn't.
-
Nov 13th, 2007, 07:39 AM
#4
Re: [2.0] "Unsafe" code?
None of these common sense caveats actually make it "unsafe". It's just MS patronising its developers (again). You'd actually have to try quite hard to make unsafe code do anything nasty without throwing a catchable exception.
That said, as soon as you start doing things like passing byte arrays back and forth between C# and C libraries (for example) you can get into a pickle where its more difficult to debug whats going on.
But that's the fun part
I don't live here any more.
-
Nov 14th, 2007, 09:51 AM
#5
Hyperactive Member
Re: [2.0] "Unsafe" code?
Along with jmcilhinney what said, I think you have to make sure that you deallocate and dispose of what ever resources you use to avoid memory leaks. Normally, .net will handle this mess for you.
My monkey wearing the fedora points and laughs at you.
-
Nov 14th, 2007, 05:54 PM
#6
Re: [2.0] "Unsafe" code?
I look at it this way. Let's say you're walking across a narrow bridge that has guard rails. You make it all the way to the other side without breaking a sweat. Now, you may never have touched the guard rails at all, but if they were removed you might still consider another crossing unsafe. Managed code includes safeguards to prevent you doing things you shouldn't. If you remove many of those safeguards then "unsafe" is not such a bad description. That said, I guess you have to ask, if .NET development is that bridge, how narrow is it really? Even if it's not very narrow, with many people crossing it at the same time those on the edges may still feel a little unsafe. For those who feel "unsafe" is a patronising term, I'd be interested to know what they'd use instead.
-
Nov 15th, 2007, 07:48 AM
#7
Re: [2.0] "Unsafe" code?
I think they only used "unsafe" because "volatile" and "unchecked" are already in use and "unprotected" would sound too much like it pertains to "protected".
I think "unguarded" might be better, or possibly "awesome"
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|