简体   繁体   English

分段错误(核心转储)-Xubuntu

[英]Segmentation fault (core dumped) - Xubuntu

I know this is asked a lot, but I couldn't find an answer that fit my situation.我知道这被问了很多,但我找不到适合我情况的答案。 I had no pointers in my code and yet I ran into this problem.我的代码中没有指针,但我遇到了这个问题。 My program is meant to factor a number, although I haven't been able to test run the program.我的程序旨在分解一个数字,尽管我无法测试运行该程序。 I am using Ubuntu 16.04 with xfce, So actually xubuntu.我在 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;
}

I don't know what is happening, as I'm not using pointers and it compiled perfectly.我不知道发生了什么,因为我没有使用指针并且它编译得很好。 Any help would be very appreciated!任何帮助将不胜感激!

When you declare the array:声明数组时:

bool stopper[wholeNum];

wholeNum is still undefined. wholeNum仍未定义。 So array stopper[] is of undefined size.所以数组stopper[]的大小未定义。 You need to first input the value of wholeNum (using cin ) and then declare the stopper[] array.您需要先输入wholeNum的值(使用cin ),然后声明stopper[]数组。 So basically, something like this:所以基本上,是这样的:

int wholeNum;
//Other lines of your code

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

Here is the successfully complied program.是成功编译的程序。

Hope this helps!希望这可以帮助!

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

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