C++-程序结束时出现分段错误
C++ - Segmentation fault at the end of program
所以我有这个程序,它运行并做它应该做的事情。但在执行结束时,它抛出了一个分段错误。我运行了调试器,它说:"程序收到信号SIGSEGV,分段错误"。我一行一行地运行调试器,当一切完成时,它似乎在主函数中抛出了错误。
代码:
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
const int MAX = 100;
int firstDigit = 0; //vertex number
int verticesCount = 0;
int adjVerticesCount = 0;
class graph
{
private:
int n;
int data[MAX];
int gptr[MAX][MAX];
public:
void create();
void cleanupMatrix();
void topological();
};
void graph::create()
{
cleanupMatrix(); // initialize the adj matrix with zeros
ifstream infile ("input.txt");
string line, line_separated;
if (infile.is_open())
{
while (infile.good())
{
while(getline(infile,line)) //reads a line
{
if(line.length() == 0)
{
n = verticesCount;
topological();
verticesCount = 0;
continue;
}
istringstream iss(line);
while(getline(iss, line_separated, ' ')) //separate numbers in each line read
{
//convert string digit to int digit
int temp;
temp = atoi(line_separated.c_str());
if(firstDigit == 0) //On each line the first digit is the vertex. The rest is the adjacency vertices
{
firstDigit = temp;
verticesCount++;
data[firstDigit] = firstDigit;
}
else
{ //pointing the g array to linked list containing adjacency vertices
adjVerticesCount++;
gptr[firstDigit][temp] = 1;
}
}
adjVerticesCount = 0;
firstDigit = 0;
}
n = verticesCount;
topological();
verticesCount = 0;
}
infile.close();
}
else
{
cout << "Error: Could not open file 'input.txt'" << endl; //oopsi.. where is the file?
}
}
void graph::cleanupMatrix()
{
int x,y;
for(x=1;x<=MAX;x++)
for(y=1;y<=MAX;y++)
gptr[x][y] = 0;
}
void graph::topological()
{
int flag;
int i,j;
int poset[MAX],included[MAX];
for(i=1;i<=n;i++)
{
poset[i]=0;
included[i]=false;
}
int k = 1;
flag = true;
int zeroindegree;
int c = 1;
while(flag==1)
{
for(i=1;i<=n;i++)
{
if(!included[i])
{
zeroindegree=true;
for(j=1;j<=n;j++)
{
if(gptr[j][i]>0)
{
zeroindegree=false;
break;
}
}
if(zeroindegree)
{
included[i]=true;
poset[k]=data[i];
k=k+1;
for(j=1;j<=n;j++)
{
gptr[i][j]=-1;
gptr[j][i]=-1;
}
break;
}
}
}
if(i==n+1)
{
if(zeroindegree==false)
{
cout<<"Graph is not acyclicn";
return;
}
else
{
poset[k]=data[i-1];
k=k+1;
flag=false;
}
}
}
ofstream outFile;
outFile.open("output.txt", ios_base::app);
cout << "Topological sorting:n";
outFile << "Topological sorting:n";
for(i=1;i<=n;i++)
{
cout << poset[i];
outFile << poset[i];
if(i != n)
{
cout << ", ";
outFile << ", ";
}
}
cout << "n" << endl;
outFile << "n" << endl;
outFile.close();
cleanupMatrix();
}
int main()
{
//Clear the output file
fstream file;
file.open("output.txt", fstream::out | fstream::trunc);
file.close();
graph obj;
obj.create();
}
我做错了什么?
您的问题可能是
int x,y;
for(x=1;x<=MAX;x++)
for(y=1;y<=MAX;y++)
gptr[x][y] = 0;
您的访问元素在数组边界之外。可能应该是:
int x,y;
for(x=0;x<MAX;x++)
for(y=0;y<MAX;y++)
gptr[x][y] = 0;
其他数组/循环也是如此。C/C++开始计数0,不同于FORTRAN开始计数1(为什么,只是为什么…(
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?