主要之前的分段故障

所以我遇到了一个问题,我的代码在我的任何主要实际运行之前导致了分段错误。 我以前从来没有遇到这种情况,而且我几乎没有四分之一的编码经验,所以我不确定是否有什么我做错了。 一切都在编译,至少在我的计算机上,但在运行它时我的主要内容从未到达。

上下文:我正在尝试在邻接矩阵中连接顶点和边缘,然后使用Prim的算法来构建MST,但这是为了以后。 我构建了一个头文件,最初只包含对结构和函数的typdef调用。 但是,我将结构定义切换到头文件,因为我遇到了内存错误; 因此,为什么我认为结构存在问题。

graph.h:

//Leland Wong 00000897031 //graph header file #include #include #include #include #ifndef GRAPH_H #define GRAPH_H typedef struct vertex { double longitude; double latitude; char city[30]; int index; int visited; //0: not visited, 1: visited, 2: visited struct edge* nexte; struct vertex* nextv; double projected; }VERTEX; typedef struct edge { struct vertex* start; struct vertex* destination; double distance; struct edge* nexte; }EDGE; typedef struct graph { struct vertex* list[756]; struct edge* matrix[756][756]; }GRAPH; /* typedef struct vertex VERTEX; typedef struct edge EDGE; typedef struct graph GRAPH; */ double findDistance(VERTEX* v1, VERTEX* v2); //compute the distance between two locations EDGE* connect(VERTEX* v1, VERTEX* v2); //connects two vertices and returns the connecting EDGE GRAPH primMatrix(GRAPH *g); //connects all vertices using Prim's Algorithm in an adjacency matrix //void lPrimConnect(VERTEX v); //connects all vertices using Prim's Algorithm in an adjacency list EDGE* findSmallestEdge(VERTEX v, GRAPH *g); //finds the smallest EDGE connected to v #endif 

graph.c:包含我所有函数的实现

 //functions //computes the distance between v1 and v2 double findDistance(VERTEX* v1, VERTEX* v2) { printf("findDistance"); double long1 = v1->longitude; double long2 = v2->longitude; double lat1 = v1->latitude; double lat2 = v2->latitude; double distance = 0; if(long1 < 0) long1 += 360; if(long2 start = v1; new->destination = v2; new->distance = findDistance(v1, v2); return new; } //finds smallest edge connected to v in GRAPH g EDGE* findSmallestEdge(VERTEX v, GRAPH *g) { printf("findSmallestEdge"); EDGE *tempe; int i, index; index = v.index; //set tempe equal to the first edge connected to v tempe = g->matrix[index][0]; //find smallest edge connected to v for(i = 0; i matrix[index][i] -> distance distance && g->list[index]->visited == 0) { tempe = g->matrix[index][i]; } } return tempe; } //creates an MST out of GRAPH g using Prim's algorithm GRAPH primMatrix(GRAPH *g) { printf("primMatrix"); GRAPH new; // = malloc(sizeof(GRAPH)); EDGE *smallest; EDGE *tempe; int i, x; i = 1; x = 0; new.list[0] = g->list[0]; //add root node to MST g->list[0]->visited = 2; smallest = findSmallestEdge(*new.list[0], g); new.matrix[0][smallest->destination->index] = smallest; //MST will contain all 756 nodes, so run this 755 times to ensure all nodes are reached while(i < 756) { x = 0; smallest = findSmallestEdge(*new.list[i], g); //i = number of vertices already reached while(x  distance  distance) { smallest = tempe; } x++; } new.list[i] = smallest -> destination; smallest -> destination -> visited = 2; new.matrix[smallest->start->index][smallest->destination->index] = smallest; i++; } return new; } 

graphmatrixmain.c:构建图形的主要function

 #include "graph.h" int main(int argc, char* argv[]) { FILE *fp; static GRAPH g; char buffer[200]; int i, j; char city[30]; char *long1; char *lat1; if(argc == 1) { printf("could not open file\n"); return 0; } else fp = fopen(argv[1], "r"); //read in line of data from txt file, build a new vertex, and insert into list while(fgets(buffer, 200, fp) != NULL) { VERTEX *new = malloc(sizeof(VERTEX)); printf("%s", buffer); sscanf(buffer, "%s %s %s", city, long1, lat1); //sscanf(buffer, "%[^\t]\t%[^\t]\t%s", city, long1, lat1); printf("scanned in data\n"); new->longitude = atof(long1); new->latitude = atof(lat1); new->index = i; g.list[i] = new; printf("%s: (%lf, %lf)", new->city, new->longitude, new->latitude); i++; } //create EDGE and make connects between every VERTEX in list for(i = 0; i < 756; i++) { for(j = 0; j nexte = g.matrix[i][j]; } } } return 0; } 

如果它是必要的,这是我正在读取的文件:cities.txt它总共包含756个条目,但就代码而言,大小不应该是相关的

 Shanghai 121.47 31.23 Bombay 72.82 18.96 Karachi 67.01 24.86 Buenos Aires -58.37 -34.61 Delhi 77.21 28.67 Istanbul 29 41.1 Manila 120.97 14.62 Sao Paulo -46.63 -23.53 Moscow 37.62 55.75 

我遇到了一个问题,我的代码在任何主要实际运行之前都会导致分段错误。

通常,这意味着main尝试放置在自动存储区域中的数据结构会溢出堆栈。 在你的情况下,看起来GRAPH是一个合适的嫌疑人:它有一个带有571536个指针的2D数组,在main有机会启动之前很可能会溢出堆栈。

这个问题的一个解决方案是将GRAPH移动到static区域:既然你在main分配它,那么无论如何它只是它的一个实例,所以声明它是静态的应该解决问题:

 static GRAPH g; 

您可能还想使用malloc在动态区域中分配它,但在这种情况下它可能无关紧要。

你说的问题不是“在主要之前”,而是在程序的前几行。 你没有初始化fp ,所以它可以去任何地方。 使用new ,循环中也会出现内存错误。 您需要将值复制到新分配的内存中。

您无法在代码中看到printf ,因为输出已缓冲,并且在刷新缓冲区之前代码崩溃。 如果你在printf("error");之后放置exit(0) printf("error"); ,你会发现它有效。