1

The problem is as the picture shows

vector<int> printMatrix(vector<vector<int> > matrix) {
    if (matrix.empty()) {
        return vector<int>();
    }
    this->matrix = std::move(matrix);
    int startRow = 0, lastRow = this->matrix.size() - 1;
    int startCol = 0, lastCol = this->matrix[0].size() - 1;
    while (startRow <= lastRow && startCol <= lastCol) {
        printCircle(startCol, lastCol, startRow, lastRow);
        ++startCol, --lastCol, ++startRow, --lastRow;
    }
}

It worked fine while variable startRow less than lastRow . In general, however, when startRow bigger than lastRow, which should be exit the while-loop but raising Exception: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) instead. I am confused about the raising exception as the picture showed.

zengtong
  • 23
  • 1
  • 4
  • Images are useful in a post, but make sure the post is still clear without them. If you post images of code or error messages, copy and paste or type the actual code or message into the post directly. – Micha Wiedenmann Jan 23 '18 at 15:13
  • The site causes browser security errors. – Cpp plus 1 Jan 23 '18 at 15:14
  • 3
    Problem seems to be that you declared printMatrix to return vector but in the function you don't return anything after the while loop. I'm surprised the code even compiles. – john Jan 23 '18 at 15:17
  • You should also to check that `matrix[0]` isn't empty. – molbdnilo Jan 23 '18 at 15:19
  • Thanks for Wiedenmann's advice :-) .Here is my first post and I'll be noticed in the future :-) . And The problem is solved , the reason is like john said. I am careless :( . – zengtong Jan 23 '18 at 15:19
  • @john It compiles with `warning: control may reach end of non-void function [-Wreturn-type]`. A good reason to not ignore warnings. – Mihayl Jan 23 '18 at 15:23
  • @A.A There is no warning before starting debug. And I did't add arguments when start debug, so was it the LLDB did ? – zengtong Jan 23 '18 at 15:32

1 Answers1

4
vector<int> printMatrix(vector<vector<int> > matrix) {
    if (matrix.empty()) {
        return vector<int>();
    }
    this->matrix = std::move(matrix);
    int startRow = 0, lastRow = this->matrix.size() - 1;
    int startCol = 0, lastCol = this->matrix[0].size() - 1;
    while (startRow <= lastRow && startCol <= lastCol) {
        printCircle(startCol, lastCol, startRow, lastRow);
        ++startCol, --lastCol, ++startRow, --lastRow;
    }
    // **** return a vector here ****
}

Need to return a vector from the function, or change it to be void.

john
  • 85,011
  • 4
  • 57
  • 81
  • strange that compiler allowed the initial version to build in the first place. good to make the compile options stricter. or check that the output isn't saying something like "control may reach end of non-void function" – dgmz Apr 20 '20 at 12:38