开发者

A variable seems to be de-referencing when I use it in a dynamic array (C++)

Here we are once again good people of the internet.

This is the code I'm using:

//This is what is in the header file
int *myArr[]; // A two-dimensional array representing holding the matrix data

//This is what is in the definition file
Matrix::Matrix(int n, int m)
{
开发者_如何转开发    myRows = n;
    myColumns = m;
    initialize();
}

void Matrix::initialize()
{
    *myArr = new int[myRows];

    for (int i=0; i < 3; i++)//Only set to 3 since myRows is acting crazy
    {
        myArr[i] = new int[myColumns];
    }
}

For some reason when I use myRows variable to create the myArr array it just seems to stop referencing the value it was pointing towards before.

For instance I give it the value 3 and after the *myArr = new int[myRows] has been executed it changes the value of myRows to 9834496, which I don't understand.

Does the "new" de-reference the variable or something? Or am I doing something wrong?

Oh and since this is a school practice project (so I won't blame you if you don't answer) I would prefer an answer over working code, so that I could know what I did wrong for future projects.


int *myArr[];

This is wrong! You've to tell the compiler the size also, of your array of pointer. How about if you declare int a[]. You're telling the compiler to create an array of int, of unknown size, which is not allowed in C++. That is why you cannot do that.

I would suggest you to do this:

int **myArr;

void Matrix::initialize()
{
    myArr = new int*[myRows]; //note int* here!

    for (int i=0; i < myRows; i++)
    {
        myArr[i] = new int[myColumns];
    }
}

This should work now.


Try replacing:

*myArr = new int[myRows];

by

myArr = new int*[myRows];


You should use std::vector<>. It deals with all the problems of memory allocation and deallocation. And it does so without any bugs.

And then you focus yourself on the real goals of your algorithm. Not on memory management :-)

typedef std::vector<int> Ints;
typedef std::vector<Ints> Matrix;
Matrix myArray;


I'm not sure if you're project requires you to use multi-level pointers, if it doesn't another way you can approach this problem is to just treat the multi-dimensional array as one big flat array.

That means when you reach the end of a row, the index after that would be the first element of the next row. Here's how the code might look:

// In this approach the double pointer int**
// is replaced with just a simple int*
int *myArr;

// Here's your Matrix ctor. Note the use of the initializer list
Matrix::Matrix(int n, int m) : myRows(n), myColumns(m)
{
    initialize();
}

void Matrix::initialize()
{
    myArr = new int[myRows * myColumns];

    /* This loop is no longer needed since we're allocating 
       one big chunk at once.
    for (int i=0; i < 3; i++)//Only set to 3 since myRows is acting crazy
    {
      myArr[i] = new int[myColumns];
    }
    */
}

// To retrieve stuff from your array
// you would do something like this:
int Matrix::operator() (const int x, const int y)
{
  return myArr[x * myRows + y];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜