加权图C-segfault
我正试图根据网络中找到的struct scheme创建自己的加权图结构,但由于某种原因,我在尝试执行代码时遇到了SEGFULT,调试器说问题出在AddVertex方法中,但不幸的是,我自己找不到它加权图C-segfault,c,memory,graph,segmentation-fault,C,Memory,Graph,Segmentation Fault,我正试图根据网络中找到的struct scheme创建自己的加权图结构,但由于某种原因,我在尝试执行代码时遇到了SEGFULT,调试器说问题出在AddVertex方法中,但不幸的是,我自己找不到它 #ifndef graph_h #define graph_h typedef struct graphCDT *graphADT; typedef struct vertexTag { char *element; int visited;
#ifndef graph_h
#define graph_h
typedef struct graphCDT
*graphADT;
typedef struct vertexTag {
char *element;
int visited;
struct edgeTag *edges;
struct vertexTag *next;
} vertexT;
typedef struct edgeTag {
int weight;
struct vertexTag *connectsTo;
struct edgeTag *next;
} edgeT;
typedef struct graphCDT {
vertexT *vertices;
} graphCDT;
#endif
#include "graph.h"
#include "dbg.h"
#include <stdlib.h>
#include <assert.h>
static int RecIsReachable(void *dest,
vertexT *startP)
{
edgeT *edgeP;
if (startP->visited)
return 0;
if (startP->element == dest)
return 1;
startP->visited = 1;
for (edgeP = startP->edges;
edgeP != NULL;
edgeP = edgeP->next) {
if (RecIsReachable(dest, edgeP->connectsTo))
return 1;
}
return 0;
}
int GraphIsReachable(graphADT graph,
void *dest,
void *source)
{
vertexT *vertP;
vertexT *startP = NULL;
for (vertP = graph->vertices;
vertP != NULL;
vertP = vertP->next) {
vertP->visited = 0;
if (vertP->element == source)
startP = vertP;
}
if (startP == NULL)
return 0;
return RecIsReachable(dest, startP);
}
void AddVertex(graphADT graph, char *element) {
if(graph == NULL) {
graph = calloc(1, sizeof(graphADT));
graph->vertices->element = element;
} else {
vertexT *temp = graph->vertices;
vertexT *n = calloc(1,sizeof(graphADT));
n->element = element;
while(temp != NULL) {
temp = temp->next;
}
temp->next = n;
n->next = NULL;
}
}
void AddEdge(graphADT graph,
int w,
char *elem1,
char *elem2) {
vertexT *to = graph->vertices;
vertexT *from = graph->vertices;
while(to != NULL) {
to = to->next;
if(to->element == elem2) {
break;
}
}
while(from != NULL) {
from = from->next;
if(from->element == elem1) {
break;
}
}
check(to->element == elem2, "Unnable to found graph which contains elem2");
check(from->element == elem1, "Unnable to found graph which contains elem1");
edgeT *temp = from->edges;
edgeT *temp2 = to->edges;
while(temp != NULL) {
temp = temp->next;
}
temp->connectsTo = to;
temp->weight = w;
while(temp2 != NULL) {
temp2 = temp2->next;
}
temp2->connectsTo = from;
temp2->weight = w;
error:
return;
}
int main()
{
char *test1 = "p";
char *test2 = "d";
char *test3 = "t";
char *test4 = "w";
char *test5 = "s";
char *test6 = "x";
graphADT graph = NULL;
AddVertex(graph, test1);
AddVertex(graph, test2);
AddVertex(graph, test3);
AddVertex(graph, test4);
AddVertex(graph, test5);
AddVertex(graph, test6);
AddEdge(graph,2,test1, test2);
AddEdge(graph,3,test1, test3);
AddEdge(graph,4,test2, test4);
AddEdge(graph,5,test5,test1);
AddEdge(graph,6,test6, test3);
return 0;
}
#ifndef图
#定义图
类型定义结构图
*graphADT;
类型定义结构vertexTag{
字符*元素;
国际访问;
结构边缘标记*边缘;
结构vertexTag*next;
}垂直文本;
类型定义结构edgeTag{
整数权重;
结构vertexTag*连接到;
结构edgeTag*下一步;
}边缘;
类型定义结构图{
vertexT*顶点;
}graphCDT;
#恩迪夫
#包括“graph.h”
#包括“dbg.h”
#包括
#包括
静态整数可接收(void*dest,
垂直文本*startP)
{
edgeT*edgeP;
如果(开始->访问)
返回0;
如果(开始->元素==dest)
返回1;
startP->visted=1;
对于(edgeP=startP->edges;
edgeP!=NULL;
edgeP=edgeP->下一步){
if(可接收(目的地、边缘->连接到目的地))
返回1;
}
返回0;
}
int graphisareable(graphhadt图,
void*dest,
空*源)
{
vertexT*vertP;
vertexT*startP=NULL;
对于(vertP=图形->顶点;
vertP!=NULL;
vertP=vertP->next){
vertP->visited=0;
if(vertP->element==源)
startP=vertP;
}
if(startP==NULL)
返回0;
返回可接收(dest、startP);
}
void AddVertex(graphhadt图,char*元素){
if(图==NULL){
graph=calloc(1,sizeof(graphADT));
图形->顶点->元素=元素;
}否则{
vertexT*temp=图形->顶点;
vertexT*n=calloc(1,sizeof(graphADT));
n->element=元素;
while(temp!=NULL){
温度=温度->下一步;
}
温度->下一步=n;
n->next=NULL;
}
}
void AddEdge(graphhadt图,
int w,
char*elem1,
字符*elem2){
vertexT*to=图形->顶点;
vertexT*from=图形->顶点;
while(to!=NULL){
to=to->next;
if(to->element==elem2){
打破
}
}
while(from!=NULL){
from=from->next;
如果(从->元素==elem1){
打破
}
}
选中(to->element==elem2,“无法找到包含elem2的图形”);
检查(from->element==elem1,“未命名以找到包含elem1的图形”);
边缘*温度=从->边缘;
边缘*temp2=至->边缘;
while(temp!=NULL){
温度=温度->下一步;
}
温度->连接到=到;
温度->重量=w;
while(temp2!=NULL){
temp2=temp2->next;
}
temp2->connectsTo=from;
temp2->weight=w;
错误:
回来
}
int main()
{
char*test1=“p”;
char*test2=“d”;
char*test3=“t”;
char*test4=“w”;
char*test5=“s”;
char*test6=“x”;
graphADT graph=NULL;
AddVertex(图,test1);
AddVertex(图,test2);
AddVertex(图,test3);
AddVertex(图,test4);
AddVertex(图,test5);
AddVertex(图,test6);
附录(图2,测试1,测试2);
附录(图3,测试1,测试3);
附录(图4、测试2、测试4);
附录(图5,测试5,测试1);
附录(图6、测试6、测试3);
返回0;
}
您的graphCDT结构有一个指向vertexT结构的指针,但在使用之前忘记为它分配内存。添加graph->vertexts=calloc(1,sizeof(vertexT))代码>添加顶点函数:
void AddVertex(graphADT graph, char *element) {
if(graph == NULL) {
graph = calloc(1, sizeof(graphADT));
graph->vertices = calloc(1, sizeof(vertexT));
graph->vertices->element = element;
} else {
<...>
void AddVertex(graphhadt图,char*元素){
if(图==NULL){
graph=calloc(1,sizeof(graphADT));
图形->顶点=calloc(1,sizeof(vertexT));
图形->顶点->元素=元素;
}否则{
快速查看一下,其他地方可能会有类似的错误,希望您能自己找到它们。使用这种类型的实现
void ResizeGraph(struct Graph *g) {
g->array = realloc(g->array , g->v+1);
if(!g->array) {
printf("Memory Error");
return;
}
g->array[g->v].head = NULL;
g->v = g->v+1;
}
另外,sizeof(graphADT)
在第一个calloc()中
是错误的;这是指针的大小,而不是结构的大小。由于当前定义的结构,这两个大小可能相同,但这仍然是一个逻辑错误。感谢您的回答,我将尝试一下。我认为为包含其他结构的结构分配内存可以处理它们中的每一个,并且不需要额外分配ary.为图形
分配内存会为顶点
成员分配空间,但它只为指针本身分配空间;它不会分配第二个顶点
可以指向的内存块。