Exit Status -1 on C++ Program

exit code minus 1
exit(-1) in c
exit() in c
exit(0) in c
c exit codes
exit 2 in c
exit 1
exit system call in c

When executed, my code gives an exit status -1. I can show the input if it makes any difference. Can anybody find why this is happening?



N 10

E 2

S 3

W 4

S 5

E 8

I have already looked at the 2D integer array, and the variables in my code, looking for uninitialized ones, but I found no such errors. Can anybody see why I am getting exit status -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++) {
        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++) {
        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++) {
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
    else {
      for (int j = 1; j < steps; j++) {
        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;

On top of excellent points made by bruno, I believe the root cause of the problem you encounter is a (nomen omen!) stack overflow.

Your array is too big to place on stack. Quick calculations (assuming sizeof(int) == 4):

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

You're trying to allocate 15.3 MiB of memory on stack, whereas, according to this question, by default Windows allows 1 MiB and Linux usually allows 8 MiB.

You should either allocate memory on the heap by yourself or (better) use std::vector, like this:

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

Compiler User Guide: Compiler exit status codes and termination , The signals SIGINT (caused by a user interrupt, like ^C) and SIGTERM (caused by a UNIX kill command) are trapped by the compiler and cause abnormal� Here, we are going to learn why an Error: Id returned 1 exit status (undefined reference to 'main') occurs and how to fixed in C programming language? Submitted by IncludeHelp , on September 11, 2018

Program Termination (The GNU C Library), The exit status provides information about why the process terminated. A process can run its own cleanup functions upon normal termination. The abort function causes abnormal program termination. The C library function void exit(int status) terminates the calling process immediately. Any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal.

Exit Codes, C/C++ Programs. The exit code is the return value from the main() or WinMain function. Visual Basic. If you are using Visual Basic .NET� A C program may also use the exit() function specifying the integer status or exit macro as the first parameter. The return value from main is passed to the exit function, which for values zero, EXIT_SUCCESS or EXIT_FAILURE may translate it to “an implementation defined form” of successful termination or unsuccessful termination .

System.Exit.Codes, Enjoy the videos and music you love, upload original content, and share it all with friends, family Duration: 9:43 Posted: May 17, 2020 If status is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment. Otherwise, the status returned depends on the system and library implementation. For a similar function that does not perform the cleanup described above, see quick_exit. Parameters status Status code. If this is 0 or EXIT_SUCCESS, it indicates

Getting exit status code in C, How can a c program can print exit() return value while it exits itself � c printing return system-calls exit. I am trying to print out the return code from� exit(1) (usually) indicates unsucessful termination. However, it's usage is non-portable. Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program. 0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value

How can a c program can print exit() return value while it exits itself , The eXtremeDB runtime returns integer codes indicating success or failure for most C API functions. The runtime returns three different types of return code:. @mwilki7 Sankyu(Thanks) Sir for the reply, just let me know one more thing Like why this works for a single string. and how does it work: I thought in the manner that being a character pointer the pointer would store 1 character as I am entering and then will move to next byte for latter entry and as soon as I press enter it puts a '\0' at the end and completes the string,

  • Save yourself a couple for loops: int field[2003][2003] = {0}; will initialize the first element of the array to 0, because that's what we asked for, and the rest to 0 because that's the default if you don't specify a value.
  • there are several problems in your code, see my answer
  • This was going on back in the 90's, too. in '93 I had a Computer Science professor tell the class that he wouldn't cover debugging because if you wrote your programs correctly you wouldn't have to debug. I thought he was just being funny until I talked to him later and found he wasn't.
  • @user4581301: Well, he was technically correct... It's also a waste of money to buy an eraser for your pencil work, because if you write down everything correctly you never have to erase anything :-)