分段断层(堆芯倾倒)-Xubuntu

Segmentation fault (core dumped) - Xubuntu

本文关键字:-Xubuntu 分段      更新时间:2023-10-16

我知道这个问题被问了很多,但我找不到适合我情况的答案。我的代码中没有指针,但我遇到了这个问题。我的程序是要考虑一个数字,尽管我还没能测试运行这个程序。我使用的是带有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;
}

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

当您声明数组时:

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.

这是成功编写的程序。

希望这能有所帮助!