-1

I have a problem with a dynamically allocated matrix. I get this error: "Exception thrown at 0x009087AC in Tema 1.exe: 0xC0000005: Access violation reading location 0xFDFDFDFD." What I'm trying to do is delete a line from a matrix:

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

I get the error in the 5th line ("matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];").

Any help would be appreciated, thanks!

  • 1
    I think when the last iteration `matrix[indexLin + 1][indexCol];` indexLin is `n - 1 ` then you use `n - 1 + ` which is UB. Reading outbound of the array. – Raindrop7 Feb 26 '18 at 19:44
  • 1
    `indexLin + 1` will be out of range on the last iteration of the outer loop. – scohe001 Feb 26 '18 at 19:45
  • You must `delete []` when you have used `new []`, or the program is undefined. – molbdnilo Feb 26 '18 at 19:45
  • @molbdnilo do you mean mixing `new[]` and `delete` causes UB? Your comment reads as if every program that leaks an array has UB... – 463035818_is_not_an_ai Feb 26 '18 at 19:51
  • 0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory: https://stackoverflow.com/a/127404/487892 – drescherjm Feb 26 '18 at 20:20
  • 1
    @user463035818 Yes, passing a pointer you did not receive from `new []` (unless it's the null pointer) to `delete[]` is undefined, as is passing a pointer you did not get from `new` to `delete` (unless it's the null pointer). – molbdnilo Feb 26 '18 at 20:27

1 Answers1

1

You are reading outbound of the array:

matrix[indexLin][indexCol] = matrix[indexLin + 1][indexCol];

So the last iteration indexLin is equal to nrLin -1 which is OK but in matrix[indexLin + 1][indexCol]; you read matrix[indexLin(-1 + 1)] which is not an elelement of the array which causes the program carsh.

You can modify it to:

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];
Raindrop7
  • 3,889
  • 3
  • 16
  • 27