当我在动态数组中使用变量时,它似乎取消引用(C++)

发布于 2024-10-12 01:32:38 字数 755 浏览 9 评论 0原文

在这里,我们再次成为互联网的好人。

这是我正在使用的代码:

//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];
    }
}

出于某种原因,当我使用 myRows 变量创建 myArr 数组时,它似乎停止引用它之前指向的值。

例如,我给它值 3,在执行 *myArr = new int[myRows] 后,它将 myRows 的值更改为 9834496,我不明白。

“新”是否取消引用变量或其他什么? 或者我做错了什么?

哦,因为这是一个学校实践项目(所以如果你不回答我不会责怪你)我更喜欢一个答案而不是工作代码,这样我就可以知道我在未来的项目中做错了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-10-19 01:32:38
int *myArr[];

这是错误的!您还必须告诉编译器指针数组的大小。如果您声明 int a[] 怎么样?您告诉编译器创建一个大小未知的 int 数组,这在 C++ 中是不允许的。这就是为什么你不能这样做。

我建议你这样做:

int **myArr;

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

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

现在应该可以了。

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.

彩虹直至黑白 2024-10-19 01:32:38

尝试替换:

*myArr = new int[myRows];

myArr = new int*[myRows];

Try replacing:

*myArr = new int[myRows];

by

myArr = new int*[myRows];
似梦非梦 2024-10-19 01:32:38

您应该使用 std::vector<>。它处理内存分配和释放的所有问题。
而且它这样做没有任何错误。

然后你专注于算法的真正目标。不涉及内存管理:-)

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

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;
生寂 2024-10-19 01:32:38

我不确定您的项目是否要求您使用多级指针,如果没有,您可以解决此问题的另一种方法是将多维数组视为一个大的平面数组。

这意味着当您到达行的末尾时,其后的索引将是下一行的第一个元素。代码如下所示:

// 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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文