动态分配矩阵上的访问冲突读取位置
Access violation reading location on dynamic allocated matrix
我对动态分配的矩阵有疑问。我收到此错误:"Exception thrown at 0x009087AC in Tema 1.exe: 0xC0000005: Access violation reading location 0xFDFDFDFD." 我想做的是从矩阵中删除一行:
void deleteLine(int **matrix, int &nrLin, int nrCol, int lineDel)
{
for (int indexLin = lineDel; indexLin < nrLin; indexLin++)
for (int indexCol = 0; indexCol < nrCol; indexCol++)
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];
nrLin--;
}
int main()
{
int **matrix, nrLines, nrColumns, lineDel;
ifstream file("datePB4.txt");
file >> nrLines>> nrColumns;
matrix= new int *[nrLines];
for (int index = 0; index < nrLines; index++)
matrix[index] = new int[nrColumns];
for (int indexLin = 0; indexLin < nrLines; indexLin++)
for (int indexCol = 0; indexCol < nrColumns; indexCol++)
file >> matrix[indexLin][indexCol];
cin >> lineDel;
deleteLine(matrix, nrLines, nrColumns, lineDel);
for (int index = 0; index < nrLines; index++)
delete matrix[index];
delete matrix;
file.close();
return 0;
}
我在第 5 行得到错误 ("matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];")。
任何帮助将不胜感激,谢谢!
您正在读取数组的出站:
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];
所以最后一次迭代 indexLin 等于 nrLin -1
,这没问题,但在 matrix[indexLin + 1][indexCol];
中你读到 matrix[indexLin(-1 + 1)]
,它不是导致程序 carsh 的数组元素。
您可以修改为:
for (int indexLin = lineDel; indexLin < nrLin - 1; indexLin++) // here solves the problem in order not to read matrix[indexLen]
for (int indexCol = 0; indexCol < nrCol; indexCol++)
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];
我对动态分配的矩阵有疑问。我收到此错误:"Exception thrown at 0x009087AC in Tema 1.exe: 0xC0000005: Access violation reading location 0xFDFDFDFD." 我想做的是从矩阵中删除一行:
void deleteLine(int **matrix, int &nrLin, int nrCol, int lineDel)
{
for (int indexLin = lineDel; indexLin < nrLin; indexLin++)
for (int indexCol = 0; indexCol < nrCol; indexCol++)
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];
nrLin--;
}
int main()
{
int **matrix, nrLines, nrColumns, lineDel;
ifstream file("datePB4.txt");
file >> nrLines>> nrColumns;
matrix= new int *[nrLines];
for (int index = 0; index < nrLines; index++)
matrix[index] = new int[nrColumns];
for (int indexLin = 0; indexLin < nrLines; indexLin++)
for (int indexCol = 0; indexCol < nrColumns; indexCol++)
file >> matrix[indexLin][indexCol];
cin >> lineDel;
deleteLine(matrix, nrLines, nrColumns, lineDel);
for (int index = 0; index < nrLines; index++)
delete matrix[index];
delete matrix;
file.close();
return 0;
}
我在第 5 行得到错误 ("matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];")。
任何帮助将不胜感激,谢谢!
您正在读取数组的出站:
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];
所以最后一次迭代 indexLin 等于 nrLin -1
,这没问题,但在 matrix[indexLin + 1][indexCol];
中你读到 matrix[indexLin(-1 + 1)]
,它不是导致程序 carsh 的数组元素。
您可以修改为:
for (int indexLin = lineDel; indexLin < nrLin - 1; indexLin++) // here solves the problem in order not to read matrix[indexLen]
for (int indexCol = 0; indexCol < nrCol; indexCol++)
matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];