c++ - 段错误(核心转储)-Xubuntu

标签 c++ linux ubuntu

我知道这个问题被问了很多,但我找不到适合我情况的答案。我的代码中没有指针,但我遇到了这个问题。我的程序旨在分解一个数字,尽管我无法测试运行该程序。我使用 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/

相关文章:

c++ - VS2008 二进制比 VS2005 慢 3 倍?

c# - C# 中的 std::string?

Mysql服务不会再重启了

python - 使用 pip 安装笔记本时出错

python - 如何获取 libicui18n.so.36 DEB?

Python 对 UbuntuOne 文件 api 的 urllib2/oauth2 请求返回 401 未授权错误

c++ - 取消引用存储在寄存器中的指针(Visual Studio)

c++ - 程序无限接受输入

linux - Autotools - 设置库路径

linux - 如何使用 wget 从网站仅下载 .exe 文件?