[.net] C++ pointer code to C# unsafe? Please help!

Started by
4 comments, last by turnpast 19 years, 2 months ago
Hello guys! I had like to port C++ code to C#. I have a question on how I should port unsafe pointer code.


        int* tiles = new int[height * width];
        // ...        

	int** tiles2d = new int*[height];

	for( int i = 0; i < height; ++i )
        {					
           tiles2d = &( tiles );
	}

</pre></div><!–ENDSCRIPT–>

I tried it:
<!–STARTSCRIPT–><!–source lang="c#"–><div class="source"><pre>

        <span class="cpp-keyword">int</span>[] tiles = <span class="vb-function">new</span> <span class="cpp-keyword">int</span>[height * width];
        <span class="cpp-comment">// …        </span>

        <span class="cpp-comment">// But what now? :/ How do I port the rest?</span>
	<span class="cpp-keyword">int</span>** tiles<span class="cpp-literal">2d</span> = <span class="vb-function">new</span> <span class="cpp-keyword">int</span>*[height];

	<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> i = <span class="cpp-literal"><span class="cpp-number">0</span></span>; i &lt; height; ++i )
        {					
           tiles<span class="cpp-literal">2d</span><span style="font-weight:bold;"> = &amp;( tiles );
	}

</pre></div><!–ENDSCRIPT–>


Please help me! :) Unsafe code is fine.

Thank you for your help,
Riddle.
Advertisement
You could just use a multi-dimensional array to achieve the same thing if I'm reading you code right.
int[,] tiles = new int[height, width];

Is this what you're wanting?
Just to show you the syntax (DON'T USE THE FOLLOWING CODE IN YOUR PROGRAM!):
unsafe {		int[] tiles = new int[height * width];	int*[] tiles2d = new int*[height];		fixed(int * ptrTiles = tiles) {		for( int i = 0; i < height; ++i ){						           tiles2d = &( ptrTiles );<br>		}<br>		<br>	}<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>But actually you can't do anything with the pointers in tiles2d. As soon as the fixed block is left, the garbage collector might decide to move 'tiles' in memory which means that the pointers in tiles2d become invalid. You have to re-design your code - e.g. simply use the method DaWanderer suggests.<br><br>Regards, <br>Andre<br>
Andre Loker | Personal blog on .NET
Another option is to create a class "Tile". Because a variable of type "Tile" is a reference instead of a value (such as an int), the same instance can be referenced in multiple locations.

public class Tile{    /* ... */}/* Elsewhere, In some function */Tile[] tiles = new Tile[height*width];foreach ( Tile t in tiles ) t = new Tile();/* ... */Tile[,] tile2D = new Tile[width,height];for ( int y=0; y<height; y++ )    for ( int x=0; x<width; x++ )         tile2D[x,y] = tiles[y*width + x];


If tile[0] is altered then tile2D[0,0] will also be altered. This will give you the same as what your doing in C++ (I think) without using 'unsafe'.
Hi!

Well, I already have a tile class these arrays contain indices into a tile sheet.
I am going to convert it to C#. :)


Bye,
Riddle.
Quote:Original post by Enfekted
If tile[0] is altered then tile2D[0,0] will also be altered. This will give you the same as what your doing in C++ (I think) without using 'unsafe'.


This is a bit dangerous because if you are using object types no tile can be reassigned in one array without also assigning it to the other. Also it will simply not work if tile is a value type.

Since the mapping operation between the views is so simple you may want to simply keep one set of data in the first or second form and provide methods to access it the different ways.

I Would recomend something like this:
class TileSet {Tile[] tiles;int width;public Tile this[int index]{get{return tiles;}}public Tile this[int x, int y]{get{return tiles[y*width+x];}}

This topic is closed to new replies.

Advertisement