分段故障(核心转储) - Xubuntu

时间:2016-05-04 03:25:43

标签: c++ linux ubuntu

我知道这被问了很多,但我无法找到适合我情况的答案。我的代码中没有指针,但我遇到了这个问题。我的程序是为了计算一个数字,虽然我还没有能够测试运行该程序。我正在使用带有xfce的Ubuntu 16.04,所以实际上是xubuntu。 main.cpp中

#include <iostream>

using namespace std;

int main(){

int wholeNum;
int newNum;
int divider = 2;
int b;
int holderNum;
int remainNum;
bool stopper[wholeNum];



cin >> wholeNum;

while (wholeNum != divider){

    holderNum = wholeNum / divider;
    remainNum = wholeNum % divider;

    if (remainNum == 0){

        if (stopper[divider] != true || stopper[holderNum] != true){
            cout << divider << " * " << holderNum << endl;
        }   
        stopper[divider] = true;
        stopper[holderNum] = true;      
    }

divider ++;
}

return 0;
}

我不知道发生了什么,因为我没有使用指针而且编译得很完美。任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:2)

声明数组时:

bool stopper[wholeNum];

wholeNum仍未定义。因此数组stopper[]的大小不确定。您需要先输入wholeNum的值(使用cin),然后声明stopper[]数组。所以基本上是这样的:

int wholeNum;
//Other lines of your code

cin>>wholeNum;
bool stopper[wholeNum]; //---> Here value of wholeNum is defined.

Edit custom shape是成功编写的程序。

希望这有帮助!