当我在动态数组中使用变量时,它似乎取消引用(C++)
在这里,我们再次成为互联网的好人。
这是我正在使用的代码:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是错误的!您还必须告诉编译器指针数组的大小。如果您声明
int a[]
怎么样?您告诉编译器创建一个大小未知的 int 数组,这在 C++ 中是不允许的。这就是为什么你不能这样做。我建议你这样做:
现在应该可以了。
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:
This should work now.
尝试替换:
由
Try replacing:
by
您应该使用 std::vector<>。它处理内存分配和释放的所有问题。
而且它这样做没有任何错误。
然后你专注于算法的真正目标。不涉及内存管理:-)
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 :-)
我不确定您的项目是否要求您使用多级指针,如果没有,您可以解决此问题的另一种方法是将多维数组视为一个大的平面数组。
这意味着当您到达行的末尾时,其后的索引将是下一行的第一个元素。代码如下所示:
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: