在这里,我们又一次成为了互联网的好人。
这是我使用的代码:
//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 = intmyRows之后,它将myRows的值更改为9834496,这是我不明白的。
"new“是否取消了对变量的引用?还是我做错了什么?
哦,因为这是一个学校实践项目(所以如果你不回答,我不会责怪你),我更喜欢答案而不是工作代码,这样我就可以知道我在未来的项目中做错了什么。
发布于 2011-01-15 00:07:56
您应该使用std::vector<>。它处理内存分配和释放的所有问题。而且它没有任何bug。
然后你专注于你的算法的真正目标。不在内存管理上:-)
typedef std::vector<int> Ints;
typedef std::vector<Ints> Matrix;
Matrix myArray;https://stackoverflow.com/questions/4693014
复制相似问题