C++ 程序的退出状态 -1

Exit Status -1 on C++ Program

执行时,我的代码给出退出状态 -1。如果有任何不同,我可以显示输入。有人能找到为什么会这样吗?

输入:

6

N 10

E 2

S 3

W 4

S 5

E 8

我已经查看了二维整数数组和代码中的变量,寻找未初始化的变量,但没有发现此类错误。任何人都可以看到为什么我得到退出状态 -1 吗?

#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main() {
  ofstream fout("mowing.out");
  ifstream fin("mowing.in");
  int n; fin >> n;
  int ans = 0;
  int field[2003][2003];
  for (int i = 0; i < 2003; i++) {
    for (int j = 0; j < 2003; j++) {
      field[i][j] = 0;
    }
  }
  int xloc = 1001, yloc = 1001, time = 0;
  for (int i = 0; i < n; i++) {
    char dir; int steps;
    fin >> dir >> steps;
    if (dir == 'N') {
      for (int j = 1; j < steps; j++) {
        yloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    if (dir == 'S') {
      for (int j = 1; j < steps; j++) {
        yloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    if (dir == 'W') {
      for (int j = 1; j < steps; j++) {
        xloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else {
      for (int j = 1; j < steps; j++) {
        xloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
  }
  if (ans == 0) fout << -1 << "\n";
  else fout << ans << "\n";
  return 0;
}

fin >> dir >> steps;

你没有得到预期的值

第一个输入是 int n; fin >> n;,如果输入文件与您在问题中指出的一样,dir 的第一个值将是换行符(在 6 之后input file) ,那么 >> 肯定会在不做任何事情的情况下保持错误,因为在 Nsteps 不兼容之后整数

解决那个问题

  • 不必混合使用 intchar 在不确定格式和显式绕过所有必要字符的情况下读取
  • 或更简单和安全不要读取 char for dir 而是一个字符串,所以 string dir; 而不是char dir; 当然,在 XN 之后,将测试 (dir == 'X') 更改为 (dir == "X")SW

可能您错过了添加一些 else 因为您这样做了 :

if (dir == 'N') {
  ...
}
if (dir == 'S') {
  ...
}
if (dir == 'W') {
  ...
}
else {
  ...
}

所以最后一个 else 通常用于案例 'E' 也用于 N 和 S 案例,可能你想要

if (dir == 'N') { // in fact (dir == "N") see remark above
  ...
}
else if (dir == 'S') { // in fact (dir == "S") see remark above
  ...
}
else if (dir == 'W') { // in fact (dir == "W") see remark above
  ...
}
else {
  ...
}

我鼓励你检查你是否成功打开了文件,目前你认为你打开了文件,并检查你是否正确阅读了输入文件

注意我的 raspberrypi 堆栈限制为 8192K (ulimit -s) 所以 field 的大小太大,我将其更改为 static 以便能够执行程序(我使用 2 for 替换了复杂的初始化)

mowing.out 的预期内容是什么?进行上述更改后我得到 18


如果我使用定义:

#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main() {
  ofstream fout("mowing.out");

  if (!fout.is_open()) {
    cerr << "cannot open mowing.out" << endl;
    return -1;
  }

  ifstream fin("mowing.in");

  if (! fin.is_open()) {
    cerr << "cannot open mowing.int" << endl;
    return -1;
  }

  int n; 

  if ((!(fin >> n)) || (n < 0)) {
    cerr << "invalid number of couples" << endl;
    return -1;
  }

  int ans = 0;
  static int field[2003][2003] = { 0};
  int xloc = 1001, yloc = 1001, time = 0;

  for (int i = 0; i < n; i++) {
    string dir; int steps;

    if (!(fin >> dir >> steps)) {
      cerr << "error while reading fin & dir" << endl;
      return -1;
    }

    if (dir == "N") {
      for (int j = 1; j < steps; j++) {
        yloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else if (dir == "S") {
      for (int j = 1; j < steps; j++) {
        yloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else if (dir == "W") {
      for (int j = 1; j < steps; j++) {
        xloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else {
      for (int j = 1; j < steps; j++) {
        xloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
  }
  if (ans == 0) fout << -1 << "\n";
  else fout << ans << "\n";
  return 0;
}

编译与执行:

pi@raspberrypi:/tmp $ g++ -g -pedantic -Wextra -Wall e.cc
pi@raspberrypi:/tmp $ cat mowing.in 
6

N 10

E 2

S 3

W 4

S 5

E 8
pi@raspberrypi:/tmp $ ./a.out
pi@raspberrypi:/tmp $ cat mowing.out 
18

除了 bruno 提出的出色观点之外,我相信您遇到的问题的根本原因是(nomen omen!)堆栈溢出。

您的数组太大无法放入堆栈。快速计算(假设 sizeof(int) == 4):

2003 * 2003 * 4 B = 16048036 B = 15671.91015625 KiB = 15.304599761962890625 MiB

您正在尝试在堆栈上分配 15.3 MiB 的内存,而根据 this question,默认情况下 Windows 允许 1 MiB,而 Linux 通常允许 8 MiB。

你应该自己在堆上分配内存或者(更好)使用std::vector,像这样:

std::vector<std::vector<int>> field (2003, std::vector(2003));
//it is already initialized above, no need for for loops ;)
//later on it can be used like regular array in most of the cases

Can anybody see why I am getting exit status -1?

不只是任何人 - 可以做到!

... 通过使用 debugger 在程序执行期间的不同点停止您的程序并检查 nans 和其他变量的值。

我假设您正在使用一些 IDE 来编辑和编译您的代码。 IDEs 通常具有集成的调试器。示例:

好像是students these days really aren't taught to debug...:-(