使用 `cin` 时`命令终止`

`Command terminated` while using `cin`

我正在使用 C++ 11 编写一个简单的代码,但如果我输入特定值,会遇到 Command terminated 错误。

密码是:

#include <iostream>
using namespace std;

int main(){
    int r, c;
    cin >> r >> c;

    int **Data = new int*[r+2]();
    for(int i=0; i < c+2; i++){
        Data[i] = new int[c+2]();
    }

    // Input Data
    for(int n=1; n<r+1; n++){
        for(int m=1; m<c+1; m++){
            cin >> Data[n][m];
        }
    }
    return 0;
}

此代码适用于大多数输入,例如:

clang++ -std=c++11 -stdlib=libc++ test.cpp -o test.out
./test.out
2 2
1 2
3 4

但是,如果我输入特定值,它会发生冲突:

clang++ -std=c++11 -stdlib=libc++ test2.cpp -o test2.out
./test2.out
5 1
1
2
3

Command terminated 

为什么会这样??

您在这里使用了错误的上限:

for(int i=0; i < c+2; i++){

应该是:

for(int i=0; i < r+2; i++){