【发布时间】:2017-03-07 14:29:02
【问题描述】:
我正在编写一个二维矩阵程序。
作业要求:
Implement the following functions:
float *allocate(int rows, int cols);
void readm(float *matrix, int rows, int cols);
void writem(float *matrix, int rows, int cols);
void mulmatrix(float *matrix1, int rows1, int cols1, float *matrix2, int cols2, float *product);
我的代码(main中删除了一些部分,只是创建和调用分配)
int main() {
float * matrix1;
float * matrix2;
matrix1 = allocate(rows1,cols1);
matrix2 = allocate(rows2,cols2);
}
float *allocate(int rows, int cols) {
float ** matrix = new float *[rows * cols];
return *matrix;
}//end allocate
void writem(float *matrix, int rows, int cols) {
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
cout << "enter contents of element at " << (x + 1) << ", " << (y + 1) << " ";
cin >> matrix[x*rows + cols];
}
}
}//end writem
我收到一个错误
在 lab5.exe 中的 0x0FECF6B6 (msvcp140d.dll) 处引发异常:0xC0000005:访问冲突写入位置 0xCDCCDCDD5。如果有这个异常的处理程序,程序可以安全地继续。
它出现在 cin >> matrix[x*rows + cols]; 行处
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
该错误表明您正在取消引用未初始化的指针。
-
同意这是一个未初始化的堆指针:stackoverflow.com/questions/127386/…
标签: c++