繁体   English   中英

访问冲突写入矩阵

[英]access violation writing matrix

我正在编写一个二维矩阵程序。

作业要求:

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);

我的代码(主要部分中的某些部分已删除,仅创建并调用allocate)

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:访问冲突写入位置0xCDCDCDD5。 如果有用于此异常的处理程序,则可以安全地继续执行该程序。

它出现在cin >> matrix [x * rows + cols]行处;

首先,您错过了索引。 应该是cin >> matrix[x*rows + y]; 代替cin >> matrix[x*rows + cols];

其次,为什么你要创建的矩阵float *而不只是float

float *allocate(int rows, int cols) {
    float* matrix = new float[rows * cols];
    return matrix;
}//end allocate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM