Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pointers in struct to itself

Pointers in struct to itself

Scheduled Pinned Locked Moved C#
csharpc++data-structureshelptutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hi! I want to define a structure that has 2 pointers to the same type as itself. For example, think of a Huffman tree node. In C++ we did it like this:

    typedef struct
    {
    unsigned int code;
    unsigned int count;
    HUFFTREENODE *l;
    HUFFTREENODE *r;
    } HUFFTREENODE;

    Now, how can we do the same in C#? I tried this:

    public struct HuffTreeNode
    {
    uint code;
    uint count;
    HuffTreeNode l;
    HuffTreeNode r;
    }

    But it gives me an error that this would create a loop in the struct... Thanks in advance and best regards Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    M 1 Reply Last reply
    0
    • D Dominik Reichl

      Hi! I want to define a structure that has 2 pointers to the same type as itself. For example, think of a Huffman tree node. In C++ we did it like this:

      typedef struct
      {
      unsigned int code;
      unsigned int count;
      HUFFTREENODE *l;
      HUFFTREENODE *r;
      } HUFFTREENODE;

      Now, how can we do the same in C#? I tried this:

      public struct HuffTreeNode
      {
      uint code;
      uint count;
      HuffTreeNode l;
      HuffTreeNode r;
      }

      But it gives me an error that this would create a loop in the struct... Thanks in advance and best regards Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      M Offline
      M Offline
      Mathew Hall
      wrote on last edited by
      #2

      You'd probably want to do something like:

      [StructLayout(LayoutKind.Sequential)]
      public struct HuffTreeNode
      {
      uint code;
      uint count;
      IntPtr leftNode;
      IntPtr rightNode;
      }

      and then use Marshal.StructureToPtr[^] and Marshal.PtrToStructure[^] to convert the struct to/from a pointer "I think I speak on behalf of everyone here when I say huh?" - Buffy

      D 1 Reply Last reply
      0
      • M Mathew Hall

        You'd probably want to do something like:

        [StructLayout(LayoutKind.Sequential)]
        public struct HuffTreeNode
        {
        uint code;
        uint count;
        IntPtr leftNode;
        IntPtr rightNode;
        }

        and then use Marshal.StructureToPtr[^] and Marshal.PtrToStructure[^] to convert the struct to/from a pointer "I think I speak on behalf of everyone here when I say huh?" - Buffy

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #3

        Thanks! Interestingly, when I replace 'struct' by 'class', the code I posted first compiles nicely (why??)... What do you think is more efficient? Making the thingy a class or leaving it as struct and using Marshal.etc? I won't have that many nodes, only about 66000... Thanks again and best regards Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        D M 2 Replies Last reply
        0
        • D Dominik Reichl

          Thanks! Interestingly, when I replace 'struct' by 'class', the code I posted first compiles nicely (why??)... What do you think is more efficient? Making the thingy a class or leaving it as struct and using Marshal.etc? I won't have that many nodes, only about 66000... Thanks again and best regards Dominik


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          Dominik Reichl wrote: What do you think is more efficient? Making the thingy a class or leaving it as struct and using Marshal.etc? I won't have that many nodes, only about 66000... Structs should (mostly) only used for Interop calls. As it seems you're designing a Huffman class library, with no interop calls, I'd create it as a class. I don't see dead pixels anymore... Yes, even I am blogging now!

          1 Reply Last reply
          0
          • D Dominik Reichl

            Thanks! Interestingly, when I replace 'struct' by 'class', the code I posted first compiles nicely (why??)... What do you think is more efficient? Making the thingy a class or leaving it as struct and using Marshal.etc? I won't have that many nodes, only about 66000... Thanks again and best regards Dominik


            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Structs are value types, classes are reference types, that's why it compiles when you use class but not when you use struct. In a class you can easily have a reference to another class (even to itself), but in a struct the compiler would have to allocate space for the contents of the struct. That's why a struct must not contain itself. Regards, mav

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups