c++exc-bad-instruction

Exception: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) using C++


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.


Solution

  • 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.