Sign in to follow this  

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

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = &( tiles[ i * width ] );
	}

I tried it:

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

        // But what now? :/ How do I port the rest?
	int** tiles2d = new int*[height];

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

Please help me! :) Unsafe code is fine. Thank you for your help, Riddle.

Share this post


Link to post
Share on other sites
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[i] = &( ptrTiles[ i * width ] );
}

}
}



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.

Regards,
Andre

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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[i];}}
public Tile this[int x, int y]{get{return tiles[y*width+x];}
}

Share this post


Link to post
Share on other sites

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this