-2

After printing a 2-dimensional array, my program crashes and I don't know why. The program crashes before "test2" is printed:

//initialising
int** matrix = new int*[x * y];
for (int i = 0; i < x; i++){
    matrix[i] = new int[y];
}
//filling with 0
for (int row = 0; row < x; row++){
     for (int cols = 0; cols < y; cols++){
          matrix [row][cols] = 0;
     }
}
//printing
for(int i = 0; i < x; ++i) {
    for(int j = 0; j < y; ++j){
       std::cout << (matrix[i][j]) << ", ";
    }
    std::cout << std::endl;
}

std::cout << "test2" << std::endl;
Shrikanth N
  • 652
  • 3
  • 17
B.abyface
  • 9
  • 4
  • 2
    What's time? And size? Why are you allocating `int** matrix = new int*[x*y];` but then only allocate `x` subpointers? – Matthieu Brucher Nov 18 '18 at 16:04
  • 1
    Possible duplicate of [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Matthieu Brucher Nov 18 '18 at 16:06
  • FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem? – user1118321 Nov 18 '18 at 16:30
  • @user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks. – Bo R Nov 18 '18 at 17:13
  • 1
    Your initilizing code is allocating too much. Should be `int **matrix = new int *[x];` (instead of x*y). And hopefully you are deallocating later on. `for (int i = 0; i < x; i++) delete [] matrix[i]; delete [] matrix;` – Bo R Nov 18 '18 at 17:20
  • @BoR That's because the calls to `delete[]` aren't shown. I assumed they existed elsewhere in the code. – user1118321 Nov 18 '18 at 17:50
  • @user1118321 And the `new int*[x*y]` why so many when you only use `x` instances? Thus only need `new int*[x]`. – Bo R Nov 18 '18 at 18:14
  • @Bo R i suspect thats what caused their crash to behin with but there is no way to verify without the OP's validation. – johnathan Nov 18 '18 at 19:03

1 Answers1

1
 #include<iostream>

 void func(int x, int y)
 {
    // initialising
    int **matrix = new int *[x];
    for (int i = 0; i < x; i++)
    {
        matrix[i] = new int[y];
    }
// filling with 0
    for (int row = 0; row < x; row++)
    {
        for (int cols = 0; cols < y; cols++)
    {
        matrix[row][cols] = 0;
    }
}
// printing
for (int i = 0; i < (x); ++i)
{
    for (int j = 0; j < (y); ++j)
    {
        std::cout << (matrix[i][j]) << ", ";
    }
    std::cout << std::endl;
}

std::cout << "test2" << std::endl;

    for(int i = 0; i < x; i++)
          delete[]matrix[i]; // clean up each y

delete[]matrix;  // clean up x
 }

 int main()
 {
    func(5, 5);
 } 

your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new [] you must call delete[] on each pointer allocated by new [] to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn

johnathan
  • 2,315
  • 13
  • 20
  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash? – user1118321 Nov 18 '18 at 17:52
  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5. – johnathan Nov 18 '18 at 17:59
  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-) – user1118321 Nov 18 '18 at 18:00