【问题标题】:Segmentation fault (core dumped) - Xubuntu分段错误(核心转储) - Xubuntu
【发布时间】:2016-05-04 03:25:43
【问题描述】:

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

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

【问题讨论】:

  • 当您甚至没有花精力自己执行代码时,您不能指望有人在您的代码中发现错误。 “请原谅我因为无法测试运行程序而犯的任何编程错误”
  • @fallenidol 由于错误,我无法执行。我只是说程序本身可能没有正确构建。我希望答案集中在主要问题上

标签: c++ linux ubuntu


【解决方案1】:

当你声明数组时:

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是编译成功的程序。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 2021-06-03
    相关资源
    最近更新 更多