【问题标题】:Segmentation fault (Core dumped) issue when running my code运行我的代码时出现分段错误(核心转储)问题
【发布时间】:2019-11-25 19:49:27
【问题描述】:

我正在做的作业,我不知道为什么当我尝试运行我的程序时,它会给出错误“分段错误(核心转储)。已经尝试了一段时间,我可以”看不出我哪里出错了。我研究了分段错误并逐行检查了我的代码,但我仍然不知道。在 c++ 中编码仍然很新。

//This program reads the last names of the candidates and the number of votes they receive. The\
 program sorts and outputs the candidates based on votes received in descending order.

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void openFile(ifstream&);
void getData(ifstream&, string [], int [], int &);
void computePercentages(int[], double [], int &);
void sortVotes(string [], int [], double [], int);
int findSmallestIndex(const int[], int, int);

const int SIZE = 30;

int main(){

   string name[SIZE];
   double percentages[SIZE];
   int votes[SIZE];
   int size, sum;
   ifstream in;

openFile(in);
   getData(in, name, votes, size);
   computePercentages(votes, percentages, sum);
   sortVotes(name, votes, percentages, size);
   for(int i = 0; i < size; i++)
      cout << name[i] << " " << votes[i] << " " << setprecision(4) << percentages[i] << endl;

   in.close();

   return (0);


}

void openFile(ifstream& in){
   string filename;
   cout << "Enter file name: ";
   cin >> filename;
   in.open(filename.c_str());
}

void getData(ifstream& in, string n[], int v[],int& s){
   int x = 0;
   for (int s = 0; s < SIZE; s++){
      n[s] = x;
      v[s] = x;
   }
   in >> n[s] >> v[s];
   while (!in.eof()){
      s++;
      in >> n[s] >> v[s];
   }
}

void computePercentages(int v[], double p[], int& sum){
   int total = 0;
   for (int i = 0; i < SIZE; i++)
      total = total + v[i];

   double factor = 100.0 / total;
   for(int i = 0; i < SIZE; i++)

      p[i] = factor * v[i];
}
void sortVotes(string n[], int v[], double p[],int s){
   for(int index = 0; s - 1; index++){
      int smallestIndex = findSmallestIndex(v, s, index)
      swap (n[index], n[smallestIndex]);
      swap (v[index], v[smallestIndex]);
      swap (p[index], p[smallestIndex]);
   }}

int findSmallestIndex(const int v[], int s, int start){
   int smallestIndex = start;
   for (int i = start +1; i < s; i++)
      if (v[i] < v[smallestIndex])
         smallestIndex = i;
   return smallestIndex;
}

void swap(string &a, string &b){
   string temp = a;
   a = b;
   b = temp;
}
void swap(int &a, int &b){
   int temp = a;
   a = b;
   b = temp;
}

void swap(double &a, double &b){
   double temp = a;
   a = b;
   b = temp;
}

就像我说的,我不知道我哪里出错了。任何见解都会有所帮助。

【问题讨论】:

  • 调用getData函数时,变量size的值是多少?
  • 传递给getdata的size变量没有初始化和使用所以
  • getData 中有两个不同的 s 变量。这是使用单字母变量名而不是描述性名称的危险。 s 应该是什么意思?您对 nv 等执行相同的操作。变量名称不会为每个字母花费任何费用。
  • 你没有给size设置一个初始值。这意味着它可以是任何东西。
  • 也许 OP 希望 getData 中的 s 设置 size 变量,因为它是通过引用传递的。当然,您会发现情况并非如此。

标签: c++ segmentation-fault coredump


【解决方案1】:

在下面的代码中,第一个循环将“s”设置为 SIZE,并且您的数组长度也是“SIZE”。

 void getData(ifstream& in, string n[], int v[],int& s){
       int x = 0;
       for (int s = 0; s < SIZE; s++){
          n[s] = x;
          v[s] = x;
       }
       in >> n[s] >> v[s];
       while (!in.eof()){
          s++;
          in >> n[s] >> v[s];
       }
    }

第二个循环 (while) 继续递增 's' (所以现在 s > SIZE),这会导致在执行 in &gt;&gt; n[s] &gt;&gt; v[s]; 时数组访问无效,因为 n[s]v[s] 现在访问超过分配的内存对于长度为 SIZE 的数组。

另外,由于这是 C++,请使用 std::arraystd::vector

【讨论】:

  • 也许 OP 的错误是 for (int s,而应该是 for (s
  • @PaulMcKenzie 我认为这只会影响输入参数int&amp; s
  • @SmexxyMoose 取决于你想要做什么,这很难从你的代码中推断出来。
猜你喜欢
  • 1970-01-01
  • 2014-09-30
  • 2013-05-04
  • 2016-03-10
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多