Easy Pointer Question

rifter1818

Well-known member
Joined
Sep 11, 2003
Messages
255
Location
A cold dark place
K here is my problem, I have a type (SQUARE)
[CS]
unsafe struct SQUARE
{
public byte Owner,Units;
public SQUARE*[] Neighbors;
public void AddUnit(byte owner)
{
byte i;
this.Units++;
this.Owner = owner;
if(Neighbors.GetUpperBound(0) < Units)
{
Units = 0;
Owner = 0;
for(i=0;i<=Neighbors.GetUpperBound(0);i++)
{
Neighbors->AddUnit(owner);
}
}
}
};
[/CS]
Now each square has some neighbors which i want to use pointers to Keep track of the problem is when i try and fill these pointers.
[CS]
IntPtr T;
Marshal.StructureToPtr(S[x,y],T,false);
S[x,y].Neighbors[0] = &(SQUARE)T.ToPointer();
[/CS]
This Cannot convert void* to SQUARE... Anyways how to i fill those pointers in C#? Thanks in advance.
 
IIRC there is no easy way to use pointers with managed types - it will introduce issues with memory management for starters.
Under normal conditions any memory allocated from the managed heap can have its address changed at runtime by the GC, this is transparent to the application though. When dealing with unmanaged memory it needs to be fixed in place and can affect the GCs ability to manage memory efficiently.
If you posted a bit more of the code regarding Neighbours then it may make things clearer.

That said my initial reaction is to ask why you are using pointers in this case anyway - if the structure (SQUARE) is a managed type and it is being called from managed code then Im struggling to see the benefits of pointers, wouldnt it be easier to store Neighbours either as a managed array or in some other structure like a HashTable or ArrayList? Creating a managed array of pointers seems an unnecessarily complex way of approaching the problem.

Would the following not be suitable
C#:
class SQUARE  //probably better using a reference type rather than a value type in this case
 {
	 public byte Owner,Units;
	 public SQUARE[] Neighbors;
	 public void AddUnit(byte owner)
	 {
		 byte i;
		 this.Units++;
		 this.Owner = owner;
		 if(Neighbors.GetUpperBound(0) < Units)
		 {
			 Units = 0;
			 Owner = 0;
			 for(i=0;i<=Neighbors.GetUpperBound(0);i++)
			 {
				 Neighbors[i].AddUnit(owner);
			 }
		 }
	 }
 }
and
C#:
S[x,y].Neighbors[0] = S[x,y];
 
Last edited by a moderator:
I have tried using the Managed Array actually, however that creates multiple copies of the Squares that arnt interconnected (Example i have created an array of squares SQUARE[6,6] S; however if i add units to S[1,1] and it "explodes" (note the part of the add unit code in the if statement) it doesnt change S[1,0] and so on only the Copys of S[1,0] that was placed in its Neighbors property. with that said i cannot have the Neighbors as a point array keeping the index of the SQUARE as this will not allways be a two dimensional array (we are just starting with this basic Square layout we will eventually be extending it onto a hex grid and various other layouts.
 
I guess the next question is how would i make an unmanaged version of the SQUARE Class/struct? The Neighbors would not have to be an undimensioned array (just have it as say 6 items then have a byte keep track of how many are actually being used... Also how do you Run Inheritance in C#?
 
It would create copies given your original code as youve declared SQUARE as being a struct and this is a value type (i.e. in use it IS the data rather than a pointer to the data).
If you declare SQUARE as being a class then it becomes a reference type and will behave how you desire (the array will contain references (similar to pointers in this respect) rather than copies)
 
Thank you

PlausiblyDamp said:
It would create copies given your original code as youve declared SQUARE as being a struct and this is a value type (i.e. in use it IS the data rather than a pointer to the data).
If you declare SQUARE as being a class then it becomes a reference type and will behave how you desire (the array will contain references (similar to pointers in this respect) rather than copies)
I wasnt Aware of the difference there with Classes/Structs Thanks for your help. Still intrested in a) Inheritance in C# and b) Creating UnManaged types in C# but youve answered my big problem. Ty.
 
If you want to create an unmanaged data type then you really need to look at C++ (or managed C++). C# allows you to interact with non-managed types but in itself it only creates managed types.
Not sure what you mean by Run Inheritance - could you give a little more detail?
 
Back
Top