我知道这个问题被问了很多,但我找不到适合我情况的答案。我的代码中没有指针,但我遇到了这个问题。我的程序旨在分解一个数字,尽管我无法测试运行该程序。我使用 Ubuntu 16.04 和 xfce,所以实际上是 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.
Here就是编译成功的程序。
希望这有帮助!
关于c++ - 段错误(核心转储)-Xubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37017848/