0

I have this code which run perfectly on Ubuntu 16.04.3 LTS. But when i build and run it via Codeblock on Windows. It's just CRASH. I don't know what i was wrong and how can i fix this problem. There are a lot of C++ program that i wrote which can run on Linux but CRASH on Windows like that.

Crashed Pricture

Thanks you guys so much for the help!

#include <iostream>

using namespace std;

int d = 1;

void topRight(int [999][999], int, int, int, int);
void bottomLeft(int [999][999], int, int, int, int);

void topRight(int a[999][999], int x1, int y1, int x2, int y2) {
  for (int i=x1;i<=x2;i++) a[y1][i]=d++;
  for (int j=y1+1;j<=y2;j++) a[j][x2]=d++;
  if (x2-x1>0 && y2-y1>0){
    y1++;
    x2--;
    bottomLeft(a,x1,y1,x2,y2);
  }
}

void bottomLeft(int a[999][999], int x1, int y1, int x2, int y2) {
    for (int i=x2;i>=x1;i--) a[y2][i]=d++;
    for (int j=y2-1;j>=y1;j--) a[j][x1]=d++;
    if (x2-x1>0 && y2-y1>0) {
        x1++;
    y2--;
        topRight(a,x1,y1,x2,y2);
    }
}

int main(void){
  int a[999][999],m,n,i,j;
  cout << "Insert n: ";
  cin >> n;
  cout << "Insert m: ";
  cin >> m;
  topRight(a,0,0,n-1,m-1);
  cout << "\nA spiral-shaped two-dimensional array whith size " << m << " x " << n << " is: \n\n";
  for(i=0;i<m;i++){
    for(j=0;j<n;j++){
      cout << a[i][j] << "  ";
    }
    cout << "\n";
  }
}

I compiled on Ubuntu terminal with this command:

g++ program.cpp -o program

And ran it with this command:

./program
lamhoangtung
  • 834
  • 2
  • 10
  • 22
  • 1
    You should not declare `int [999][999]` as function parameters because it does not do what you think it does and you should not create large arrays such as `int a[999][999]` on the stack. – user7860670 Dec 17 '17 at 13:28
  • 2
    Learn to use your debugger. – Richard Critten Dec 17 '17 at 13:29
  • 3
    Compile with -O3 -g on linux. It may crash then. Use the debugger to find out where. –  Dec 17 '17 at 13:31
  • Recommended read: https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr – user0042 Dec 17 '17 at 13:33
  • I suggest for the rest of your life to initialize all your variables and members. 0xC00000FD = stack overflow, which means your recursions are getting out of hand. – StarShine Dec 17 '17 at 13:36
  • 3
    Just a guess, but you are allocating a million ints on the stack. That's 4mib. Probably too much for Windows (a quick search showed that the stack on win can be 1mib). Try to put that data onto the heap. – Adam Dec 17 '17 at 13:39
  • I.e. use an std:: vector – Adam Dec 17 '17 at 13:41
  • here are the stack sizes: https://cs.nyu.edu/exact/core/doc/stackOverflow.txt – Adam Dec 17 '17 at 13:46
  • Thanks you guys for the help. I'm going to try it tommorow <3 – lamhoangtung Dec 17 '17 at 16:04

1 Answers1

3

When you declare 999x999 matrix, with simple math:

999*999 = 998001

An integer holds 4 byte in memory, so

998001*4 = 3992004

Almost equals to 4*10^6 byte. When u declare a variable in your main function, it tries to take memory from the stack. In stack, you can not give such that memory. Thats why you are getting stackoverflow error.

Try to reduce the sizes of your matrice or declare this variable as global. But global variables also have limit.

Yavuz Koca
  • 455
  • 4
  • 17