【发布时间】:2017-12-17 13:26:25
【问题描述】:
我的这段代码可以在 Ubuntu 16.04.3 LTS 上完美运行。但是当我在 Windows 上通过 Codeblock 构建和运行它时。这只是崩溃。我不知道我错了什么,我该如何解决这个问题。我写的很多 C++ 程序都可以在 Linux 上运行,但在 Windows 上就这样崩溃了。
非常感谢你们的帮助!
#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";
}
}
我用这个命令在 Ubuntu 终端上编译:
g++ program.cpp -o program
然后用这个命令运行它:
./program
【问题讨论】:
-
你不应该将
int [999][999]声明为函数参数,因为它不会做你认为它做的事情,你不应该在堆栈上创建像int a[999][999]这样的大数组。 -
学习使用调试器。
-
在 linux 上使用 -O3 -g 编译。那时它可能会崩溃。使用调试器找出位置。
-
只是猜测,但您在堆栈上分配了一百万个整数。那是4mib。对于 Windows 来说可能太多了(快速搜索显示 win 上的堆栈可以是 1mib)。尝试将这些数据放入堆中。
标签: c++ linux windows g++ codeblocks