Pointers in struct to itself
-
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) -
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)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
-
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
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) -
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)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!
-
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)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