0

I'm developing a 2d-platformer. Everything was fine until I've got some hard to solve problem. Level map is stored in dynamic multidemension array(char **map). It works fine, until I want to redefine it

Here's the part of code:

Map& Map::operator=(const Map& rhs)
{
    if(width!=0||height!=0)
    {
        for(int i=0;i<width;i++)
            delete[] map[i];
        delete[] map;
    } //deleting previously created array

    height=rhs.height;
    width=rhs.width; //receiving other map's size

    map=new char* [width];
    walkmap=new unsigned char* [width];
    objmap=new char* [width];
    for(int i=0;i<width;i++)
    {
        *(map+i)=new char[height];
    } //creating new array

    for(int h=0;h<height;h++)
        for(int w=0;w<width;w++)
        {
            map[w][h]=rhs.map[w][h];
        } //receiving new values

    //...
}

Everything works fine for the first time, but when I need to redefine array for the second time my program crashes at the part, when array is receiving new values from another one. May be I miss something, but I can't find it! I was searching for this problem, but didn't find what I am doing wrong. Help me, please.

2
  • You've left off the declaration of the type(def?) Map, which is rather important to the question. Commented May 10, 2011 at 13:38
  • Also I would change *(map+i) to map[i] just because its much more descriptive of what you are trying to accomplish with that line IMHO. Commented May 10, 2011 at 13:41

2 Answers 2

1

As always, Boost has an elegant and memory efficient multi-dimensional array class:

http://www.boost.org/doc/libs/1_39_0/libs/multi_array/doc/user.html

For example, to setup a 10 x 20 array of bool values:

 
    boost::multi_array  mtaFlagMatrix(boost::extents[10][20]);
 

Then to access its elements:

 
    mtaFlagMatrix[2][6] = false; // indexes are zero-based - just like normal C arrays
     ...
    if ( mtaFlagMatrix[2][6] )
    {
       ...
    }
 

Then, you can resize the array this way (existing values are preserved):

 
typedef boost::multi_array array_type;

array_type::extent_gen extents; array_type A(extents[3][3][3]); A[0][0][0] = 4; A[2][2][2] = 5; A.resize(extents[2][3][4]); assert(A[0][0][0] == 4); // A[2][2][2] is no longer valid.

This saved me a lot of time testing for extreme cases.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I don't want use additional components, because my code'll be hard to undrerstand. I don't need to change the size very often. Just 2 or 3 times! And using libraries won't help me to understand, when I was wrong D:
0

Your 2d array is not freeed properly. I advise you to use the Iliffe way of allocating 2d arrays which is faster and safer to use:

char** alloc_array( int h, int w )
{
  typedef char* cptr;
  int i;
  char** m = new cptr[h];
  m[0] = new char[h*w];
  for(i=1;i<h;i++) m[i] = m[i-1]+w;
  return m;
}

void release_array(char** m)
{
  delete[] m[0];
  delete[] m;
}

int main()
{
  int r,c;
  char** tab;
  int width  = 5;
  int height = 3;
  tab = alloc_array(height, width); /* column first */

  for(r = 0;r<height;++r)
   for(c = 0;c<width;++c)
    tab[r][c] = (1+r+c);

  for(r = 0;r<height;++r)
  {
    for(c = 0;c<width;++c)
    {
      printf("%d\t",tab[r][c]);
    }
    puts("");
  }
  release_array(tab);
}

This can be easily encapsulated in a neat 2d-array class or made to use std::vector instead of raw allocation. Prefer this way of doing 2d array as it is cache friendly , style provide the [][] access and is no slower and soemtimes faster than the polynomial 1d access.

5 Comments

Thanks, I'll try to use this method
Aww, same problem. It works fine for the first time, then I delete previously created array, create new array(everything is okay now), but when I start to assign some new values to the new array program crashes again, same is in my code.
you'r doign something wrong elsewhere. can you show me your updated code usign these functions
pastebin.com/Gtvnf2wA Here it is. (For the first time rhs.tileset!=0, but fir the second it is 0
Oh, and the other arrays use the same method(but it's not shown in the code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.