简体   繁体   English

C ++程序可以在Linux上完美运行,但不能在Windows上运行

[英]C++ program run perfectly on Linux but can't run on Windows

I have this code which run perfectly on Ubuntu 16.04.3 LTS. 我有这段代码可以在Ubuntu 16.04.3 LTS上完美运行。 But when i build and run it via Codeblock on Windows. 但是当我在Windows上通过Codeblock构建和运行它时。 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. 我写了很多C ++程序,它们可以在Linux上运行,但在Windows上可以像这样崩溃。

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: 我在Ubuntu终端上使用以下命令进行编译:

g++ program.cpp -o program

And ran it with this command: 并使用以下命令运行它:

./program

When you declare 999x999 matrix, with simple math: 使用简单的数学方法声明999x999矩阵时:

999*999 = 998001

An integer holds 4 byte in memory, so 整数在内存中保留4个字节,因此

998001*4 = 3992004

Almost equals to 4*10^6 byte. 几乎等于4 * 10 ^ 6字节。 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. 这就是为什么您遇到stackoverflow错误的原因。

Try to reduce the sizes of your matrice or declare this variable as global. 尝试减小矩阵的大小,或将此变量声明为global。 But global variables also have limit. 但是全局变量也有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM