为什么在尝试编写图形时会出现分段错误?
Why do I get a segmentation fault when trying to write a graph?
我的任务是阅读这种输入格式的图表:
并以此格式输出
但是,当我 运行 我的程序时,我总是遇到分段错误。我认为我的问题是在写图表时,但我似乎无法找出原因。有人能给我指出正确的方向吗?
更多信息:readGraph 必须使用 insertEdge 来插入一条边。
无论如何,每行读三个数字是很诱人的。最后一行只会读取一个数字成功。但是程序往往会被修改,而且,在工作量不大的地方,为修改做好准备是个好主意。如果更改程序以便在图形之后有更多输入怎么办?您不希望 readGraph 读取图表后面的内容。
编写 readGraph,使其不依赖于仅包含 0 的行是输入中的最后一个内容。这很容易做到。阅读第一个数字并在阅读接下来的两个之前检查它。
struct Edge
{
int vertex1;
int vertex2;
int weight;
Edge()
{
vertex1 = 0;
vertex2 = 0;
weight = 0;
}
};
struct Graph
{
int numOfVertices;
int numOfEdges;
Edge* edges;
int sizeOfArray;
Graph(int n, int e)
{
numOfVertices = n;
numOfEdges = 0;
sizeOfArray = e;
edges = new Edge[e];
}
};
//Inserts an edge between vertices u and v, of weight w, into graph g.
void insertEdge(int u, int v, int w, Graph* g)
{
Edge e;
e.vertex1 = u;
e.vertex2 = v;
e.weight = w;
g->edges[g->numOfEdges] = e;
g->numOfEdges++;
}
//Reads vertices, edges, and weight from the input
//and allocates a graph in the heap with enough room for e edges.
Graph* readGraph(int e)
{
int numberOfVertices, edge;
scanf("%i", &numberOfVertices);
Graph* g = new Graph(numberOfVertices, e);
int u, v, w;
while(scanf("%i", &edge) != 0)
{
scanf("%i%i%i", &u, &v, &w);
insertEdge(u,v,w,g);
}
return g;
}
//Writes graph g by listing the number of vertices and the number of edges.
void writeGraph(const Graph* g)
{
printf("There are %i vertices and %i edges", g->numOfVertices, g->numOfEdges);
printf("Vertices Weight");
for(int i = 0; i < g->numOfEdges; i++)
{
printf(" %i %i %i", g->edges[i].vertex1, g->edges[i].vertex2, g->edges[i].weight);
}
}
int main()
{
int maxEdges = 1000;
Graph* g = readGraph(maxEdges);
writeGraph(g);
return 0;
}
我没有发现你的代码有问题,但我可能是瞎了眼。尽管如此,您可以使用 gdb 进行调试。投入 15 分钟:https://www.youtube.com/watch?v=PorfLSr3DDI
或者您可以使用像 Valgrind 这样的工具:https://valgrind.org/,
https://valgrind.org/docs/manual/quick-start.html
祝你一切顺利。
由于 GDB 中一些很好的旧打印语句,我实际上找到了答案。在 readGraph 中,scanf 首先扫描到边缘变量并存储变量。因此,下一个读取的数字将不是实际的第一个数字,从而导致无限循环(分段错误),因为根据输入,0 被读取为图形的一部分,后来再也找不到了。只需将扫描更改为 scanf("%i%i", &v, &w);并在 insertEdge() 中使用已读取的 u 将正确读取图形。
我的任务是阅读这种输入格式的图表:
并以此格式输出
但是,当我 运行 我的程序时,我总是遇到分段错误。我认为我的问题是在写图表时,但我似乎无法找出原因。有人能给我指出正确的方向吗?
更多信息:readGraph 必须使用 insertEdge 来插入一条边。 无论如何,每行读三个数字是很诱人的。最后一行只会读取一个数字成功。但是程序往往会被修改,而且,在工作量不大的地方,为修改做好准备是个好主意。如果更改程序以便在图形之后有更多输入怎么办?您不希望 readGraph 读取图表后面的内容。
编写 readGraph,使其不依赖于仅包含 0 的行是输入中的最后一个内容。这很容易做到。阅读第一个数字并在阅读接下来的两个之前检查它。
struct Edge
{
int vertex1;
int vertex2;
int weight;
Edge()
{
vertex1 = 0;
vertex2 = 0;
weight = 0;
}
};
struct Graph
{
int numOfVertices;
int numOfEdges;
Edge* edges;
int sizeOfArray;
Graph(int n, int e)
{
numOfVertices = n;
numOfEdges = 0;
sizeOfArray = e;
edges = new Edge[e];
}
};
//Inserts an edge between vertices u and v, of weight w, into graph g.
void insertEdge(int u, int v, int w, Graph* g)
{
Edge e;
e.vertex1 = u;
e.vertex2 = v;
e.weight = w;
g->edges[g->numOfEdges] = e;
g->numOfEdges++;
}
//Reads vertices, edges, and weight from the input
//and allocates a graph in the heap with enough room for e edges.
Graph* readGraph(int e)
{
int numberOfVertices, edge;
scanf("%i", &numberOfVertices);
Graph* g = new Graph(numberOfVertices, e);
int u, v, w;
while(scanf("%i", &edge) != 0)
{
scanf("%i%i%i", &u, &v, &w);
insertEdge(u,v,w,g);
}
return g;
}
//Writes graph g by listing the number of vertices and the number of edges.
void writeGraph(const Graph* g)
{
printf("There are %i vertices and %i edges", g->numOfVertices, g->numOfEdges);
printf("Vertices Weight");
for(int i = 0; i < g->numOfEdges; i++)
{
printf(" %i %i %i", g->edges[i].vertex1, g->edges[i].vertex2, g->edges[i].weight);
}
}
int main()
{
int maxEdges = 1000;
Graph* g = readGraph(maxEdges);
writeGraph(g);
return 0;
}
我没有发现你的代码有问题,但我可能是瞎了眼。尽管如此,您可以使用 gdb 进行调试。投入 15 分钟:https://www.youtube.com/watch?v=PorfLSr3DDI
或者您可以使用像 Valgrind 这样的工具:https://valgrind.org/, https://valgrind.org/docs/manual/quick-start.html
祝你一切顺利。
由于 GDB 中一些很好的旧打印语句,我实际上找到了答案。在 readGraph 中,scanf 首先扫描到边缘变量并存储变量。因此,下一个读取的数字将不是实际的第一个数字,从而导致无限循环(分段错误),因为根据输入,0 被读取为图形的一部分,后来再也找不到了。只需将扫描更改为 scanf("%i%i", &v, &w);并在 insertEdge() 中使用已读取的 u 将正确读取图形。