Results 1 to 7 of 7

Thread: [2.0] "Unsafe" code?

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    [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?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] "Unsafe" code?

    Yes.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  5. #5
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
  •  



Click Here to Expand Forum to Full Width