Evilness of unsafe c# menu

User Tag List

Results 1 to 13 of 13
  1. #1
    Kryso's Avatar Active Member
    Reputation
    40
    Join Date
    Jul 2009
    Posts
    97
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Evilness of unsafe c#

    I haven't seen much examples of unsafe code on these forums and I think I've read somewhere here that it's evil, however I can't really figure out why. I'm now experimenting with directx in my tool and unsafe code is actually helping me a lot. So far the only thing I have found against using it is harder debugging, however I don't know what exactly it does under the hood.

    For example this:
    Code:
    fixed ( Line* pBuffer = lines )
        device.DrawPrimitiveUP( D3DPrimitiveType.LINELIST, lines.Length, pBuffer, sizeof( Line ) / 2 );
    looks much simpler than it's safe alternative wich would probably look like this:
    Code:
    int size = Marshal.SizeOf( typeof( Line ) );
    IntPtr pMemory = Marshal.AllocHGlobal( size * lines.Length );
    try {
        for ( int i = 0; i < lines.Length; i++ )
            Marshal.StructureToPtr( lines[ i ], pMemory.Offset( i * size ), false );
    
        device.DrawPrimitiveUP( D3DPrimitiveType.LINELIST, lines.Length, pMemory, size / 2 );
    }
    finally {
        Marshal.FreeHGlobal( pMemory );
    }
    this is probably wrong forum, but I'm afraid nobody reads the c# section so sorry about that

    Evilness of unsafe c#
  2. #2
    kynox's Avatar Member
    Reputation
    830
    Join Date
    Dec 2006
    Posts
    888
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What the jesus? lines.ToArray()

  3. #3
    Kryso's Avatar Active Member
    Reputation
    40
    Join Date
    Jul 2009
    Posts
    97
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    variable "lines" = array of type "Line"
    type "Line" = 2 vertexes

    So you are suggesting put into DrawPrimitiveUP delegate "ref Line[]" or better "ref Vertex[]"? Well that would work, but it would limit me to only one vertex type. I'm probably not gonna use more that one for my current needs, but still I would like it to be more generic

  4. #4
    kynox's Avatar Member
    Reputation
    830
    Join Date
    Dec 2006
    Posts
    888
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Kryso View Post
    variable "lines" = array of type "Line"
    type "Line" = 2 vertexes

    So you are suggesting put into DrawPrimitiveUP delegate "ref Line[]" or better "ref Vertex[]"? Well that would work, but it would limit me to only one vertex type. I'm probably not gonna use more that one for my current needs, but still I would like it to be more generic
    What you're doing is just overcomplicating things. The function is generic..

  5. #5
    amadmonk's Avatar Active Member
    Reputation
    124
    Join Date
    Apr 2008
    Posts
    772
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this code sample may have been silly, but the general point that unsafe C# often simplifies things (by not having to deal with GC, by allowing you to directly deref a pointer to a struct, etc.) is still valid

    I guess I just assumed everyone knew that...
    Don't believe everything you think.

  6. #6
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't C++/CLI be a better solution for you if you like unsafe C#?
    Hey, it compiles! Ship it!

  7. #7
    Kryso's Avatar Active Member
    Reputation
    40
    Join Date
    Jul 2009
    Posts
    97
    Thanks G/R
    0/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Amadmonk: thanks, thats exactly what I wanted to hear

    However I still can't figure out why my example is bad. Lets say I want to make wrapper for IDirect3DDevice9. How can I define safe delegate that accepts different struct arrays as one parameter without Marshal.StructureToPtr? For example my vertex can look both like this:
    Code:
    struct VertexXYZD {
        Vector3 vector;
        Color diffuse;
    }
    and this:
    Code:
    struct VertexXYZND {
        Vector3 vector;
        Vector3 normal;
        Color diffuse;
    }
    I was thinking about using System.Array type, but that can be marshalled only into SafeArray and I need LPArray.. so what em I missing?


    flo8464: maybe, but this looks like fun actually
    Last edited by Kryso; 01-26-2010 at 07:56 PM.

  8. #8
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would still need to 'decorate' the struct with the marshal attributes. .NET still needs to be aware of how the struct is mapped to memory to be able to dereference it from a pointer.

  9. #9
    vulcanaoc's Avatar Member
    Reputation
    31
    Join Date
    Jul 2008
    Posts
    125
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by flo8464 View Post
    Wouldn't C++/CLI be a better solution for you if you like unsafe C#?
    C++/CLI is a pretty ugly thing.

  10. #10
    amadmonk's Avatar Active Member
    Reputation
    124
    Join Date
    Apr 2008
    Posts
    772
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by vulcanaoc View Post
    C++/CLI is a pretty ugly thing.
    I'm actually slowly learning to love it.

    I know Cypher will laugh, but the truth is that for some tasks, it's the best way to bind native and .Net stuff together.

    Purist C++ coders will hate it, of course, and there are parts that I'm not fond of, but... it's not that bad
    Don't believe everything you think.

  11. #11
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by amadmonk View Post
    I'm actually slowly learning to love it.

    I know Cypher will laugh, but the truth is that for some tasks, it's the best way to bind native and .Net stuff together.

    Purist C++ coders will hate it, of course, and there are parts that I'm not fond of, but... it's not that bad
    Oh, I totally agree it's the most appropriate language for certain tasks. It's just one of those things you love to hate though.

  12. #12
    MaiN's Avatar Elite User
    Reputation
    335
    Join Date
    Sep 2006
    Posts
    1,047
    Thanks G/R
    0/10
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cypher View Post
    Oh, I totally agree it's the most appropriate language for certain tasks. It's just one of those things you love to hate though.
    Cypher (Chazwazza) @ MMOwned: The fact that you have to use C++/CLI to wrap C++ code to make it available to C#
    Cypher (Chazwazza) @ MMOwned: is a mess
    Cypher (Chazwazza) @ MMOwned: that's just awful
    Cypher (Chazwazza) @ MMOwned: so
    Cypher (Chazwazza) @ MMOwned: fail
    Cypher to me at wrapping Recast and Detour with C++/CLI.
    [16:15:41] Cypher: caus the CPU is a dick
    [16:16:07] kynox: CPU is mad
    [16:16:15] Cypher: CPU is all like
    [16:16:16] Cypher: whatever, i do what i want

  13. #13
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by MaiN View Post
    Cypher to me at wrapping Recast and Detour with C++/CLI.
    WHOOOSH!!!!1111oneoneone

Similar Threads

  1. Evil Addon Website
    By Bobrick22 in forum World of Warcraft General
    Replies: 0
    Last Post: 05-07-2007, 05:17 PM
  2. Associate Professor Evil Kills All Beggars
    By Alkhara Majere in forum Screenshot & Video Showoff
    Replies: 0
    Last Post: 05-03-2007, 09:25 PM
  3. Black Stalion - Evil PVP warhorse
    By Blarkman11 in forum WoW ME Questions and Requests
    Replies: 3
    Last Post: 01-22-2007, 09:19 AM
  4. Very evil model editing..
    By dumbledrew in forum World of Warcraft Model Editing
    Replies: 7
    Last Post: 11-05-2006, 12:09 PM
  5. Need To Do Something Really Evil To A Player
    By Niko33 in forum World of Warcraft General
    Replies: 13
    Last Post: 10-15-2006, 03:35 PM
All times are GMT -5. The time now is 07:48 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search