前主段错误 [英] Segmentation Fault before main
问题描述
所以我一直运行到哪里不知我的code为之前的任何我的主要的分割造成的故障问题实际运行。我从来没有过这种情况发生前,我几乎没有编码经验的四分之一的价值,所以我不知道是否有什么东西我做错了。一切编译,至少在我的电脑上,而是运行它我的主要是从来没有达到时。
上下文:我想点和边连接的邻接矩阵,然后用Prim算法构建MST,但这是供以后使用。我建了一个头文件,它最初包含只为结构和功能typdef调用。不过,我切换结构定义的头文件,因为我是越来越内存错误;因此为什么我觉得有一个问题与结构。
graph.h:
//利兰黄00000897031
//图形头文件#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&MATH.H GT;的#ifndef GRAPH_H
#定义GRAPH_Htypedef结构顶点
{
双经度;
双纬度;
焦城[30];
INT指数;
INT参观; // 0:没有去过,1:走访,2:参观
结构边缘*NEXTë;
结构顶点* NexTVTM方案;
双投射;
}顶点;
typedef结构边缘
{
结构顶点*启动;
结构顶点*目的地;
双距离;
结构边缘*NEXTë;
}边缘;
typedef结构图
{
结构顶点*名单[756];
结构边缘*矩阵[756] [756];
}图形;
/ *
typedef结构顶点顶点;
typedef结构边缘上的边缘;
typedef结构图图;
* /双findDistance(VERTEX * V1,顶点V2 *); //计算两个位置之间的距离
EDGE *连接(VERTEX * V1,顶点V2 *); //连接两个顶点和返回连接边缘
GRAPH primMatrix(GRAPH * G); //连接使用Prim算法的所有顶点的邻接矩阵
//无效lPrimConnect(顶点v); //连接使用Prim算法的所有顶点在邻接表
EDGE * findSmallestEdge(顶点v,图形* G); //发现连接到V最小的EDGE
#万一
graph.c:包含了我所有的函数的实现
//功能//计算v1和v2之间的距离
双findDistance(VERTEX * V1,VERTEX * V2)
{
的printf(findDistance);
双long1 = V1->经度;
双long2 = V2->经度;
双LAT1 = V1->纬度;
双LAT2 = V2->纬度;
双距离= 0; 如果(long1℃,)
long1 + = 360;
如果(long2℃,)
long2 + = 360; 距离=则函数powf((long1-long2),2)+则函数powf((LAT1 - LAT2),2);
距离=开方(距离);
返回距离;
}//创建并返回一个连接v1和v2的边缘
EDGE *连接(VERTEX * V1,VERTEX * V2)
{
的printf(连接);
EDGE *新的;
新建 - >开始= V1;
新建 - >目标= V2;
新建 - >距离= findDistance(V1,V2);
返回新;
}
//发现图g连接到V最小边缘
EDGE * findSmallestEdge(顶点v,图形* G)
{
的printf(findSmallestEdge);
EDGE *坦佩;
INT I,指数;
指数= v.index; //设置坦佩等于连接到v的第一边缘
坦佩= G->矩阵[指数] [0];
//找到连接到V最小边缘
对于(i = 0; I< 756;我++)
{
如果(G->矩阵[指数] [I] - >距离< tempe->距离和放大器;&安培; G->清单[指数] - >参观== 0)
{
坦佩= G->矩阵[指数] [I];
}
}
返回坦佩;
}//使用Prim算法创建MST出图G
GRAPH primMatrix(GRAPH * G)
{
的printf(primMatrix);
GRAPH新; // =的malloc(sizeof的(图));
EDGE *最小;
EDGE *坦佩;
INT I,X;
I = 1;
X = 0; new.list [0] = G->清单[0]; //根节点添加到MST
G->清单[0] - >参观= 2;
最小= findSmallestEdge(* new.list [0],G);
new.matrix [0] [smallest-> destination->指数=最小;
// MST将包含所有756个节点,那么运行这个755次,以确保所有节点都达到
而(I< 756)
{
X = 0;
最小= findSmallestEdge(* new.list [I],G);
// I =顶点的数量已经达到
而(X< I)
{
坦佩= findSmallestEdge(* new.list [X],G);
如果(坦佩 - >距离<最小 - >距离)
{
最小=坦佩;
}
X ++;
}
new.list [i] =最小 - >目的地;
最小 - >目的地 - >访问= 2;
new.matrix [smallest->开始 - >指数] [smallest-> destination->指数=最小;
我++;
}
返回新;
}
graphmatrixmain.c:我的主要功能,构建图
的#includegraph.hINT主(INT ARGC,CHAR *的argv [])
{
FILE * FP;
静态图g;
字符缓冲区[200];
INT I,J;
焦城[30];
字符* long1;
字符* LAT1; 如果(的argc == 1)
{
的printf(无法打开文件\\ n);
返回0;
} 其他
FP = FOPEN(的argv [1],R); //读取从txt文件中的数据线,建立一个新的顶点,并插入到列表
而(与fgets(缓冲液,200,FP)!= NULL)
{
VERTEX *新的malloc =(sizeof的(顶点));
的printf(%S,缓冲区);
sscanf的(缓冲,%s%s%S,市,long1,LAT1);
// sscanf的(缓冲,%[^ \\ t] \\ T%[^ \\ t] \\ t%S,市,long1,LAT1);
的printf(数据\\ n扫描);
新建 - >经度= ATOF(long1);
新建 - >纬度= ATOF(LAT1);
新建 - >指数= I;
g.list [I] =新的;
的printf(%S(%LF,LF%),新建 - >城市,新 - >经度,新建 - >纬度);
我++;
}
//创建EDGE,并在列表中的每个顶点之间进行连接
对于(i = 0; I< 756;我++)
{
为(J = 0; J< 756; J ++)
{
g.matrix [I] [J] =连接(g.list [I],g.list [J]);
如果(J == 0)
{
g.list [I] - >NEXTë= g.matrix [I] [J]。
}
}
}
返回0;
}
在情况下,其必须的,这是我从我读文件:cities.txt
它包含了756项总,但至于code而言大小不应该是相关的
上海121.47 31.23
孟买72.82 18.96
卡拉奇67.01 24.86
布宜诺斯艾利斯-58.37 -34.61
德里77.21 28.67
伊斯坦布尔29 41.1
马尼拉120.97 14.62
圣保罗-46.63 -23.53
莫斯科37.62 55.75
我已经运行到哪里不知我的code为之前的任何我的主要实际运行导致段故障问题。
块引用>通常情况下,这意味着你的
主
试图将数据结构中的自动存储区溢出堆放置。在您的情况,它看起来像GRAPH
是一个合适的犯罪嫌疑人来做到这一点:它与571536指针二维数组,这可能之前很好使栈溢出,你的主
都有机会下手。一个解决这个问题,将移动
GRAPH
到静态
区:因为你在分配它主
,这将是它的只有一个实例,无论如何,所以声明为静态应该解决这个问题:静态图g;
您可能还需要使用
的malloc
分配它在动态区域,但在这种情况下,它可能并不重要。so I've been running into a problem where somehow my code is causing segmentation faults before any of my main actually runs. I've never had this happen before and I hardly have a quarter's worth of coding experience so I'm not sure if there's something I'm doing wrong. Everything compiles, at least on my computer, but upon running it my main is never reached.
Context: I'm trying to connect Vertices and Edges in an adjacency matrix and then use Prim's algorithm to build an MST, but that's for later. I built a header file, which originally contained only typdef calls for the structures and the functions. However, I switched the structure definitions to the header file because I was getting memory errors; hence why I think there's an issue with the structs.
graph.h:
//Leland Wong 00000897031 //graph header file #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #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: contains the implementations of all my functions
//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 < 0) long2 += 360; distance = powf((long1-long2), 2) + powf((lat1 - lat2), 2); distance = sqrt(distance); return distance; } //creates and returns an edge that connects v1 and v2 EDGE* connect(VERTEX* v1, VERTEX* v2) { printf("connect"); EDGE *new; new->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 < 756; i++) { if(g->matrix[index][i] -> distance < tempe->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 < i) { tempe = findSmallestEdge(*new.list[x], g); if(tempe -> distance < smallest -> 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: my main function which builds the graphs
#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 < 756; j++) { g.matrix[i][j] = connect(g.list[i], g.list[j]); if(j == 0) { g.list[i]->nexte = g.matrix[i][j]; } } } return 0; }
In case its necessary, this is the file i'm reading in from: cities.txt it contains 756 entries total but as far as the code is concerned size shouldn't be relevant
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
解决方案I've been running into a problem where somehow my code is causing segmentation faults before any of my main actually runs.
Usually, this means that the data structures that your
main
tries to place in the automatic storage area overflow the stack. In your situation, it looks like theGRAPH
is a suitable suspect to do just that: it has a 2D array with 571536 pointers, which could very well overflow the stack before yourmain
gets a chance to start.One solution to this problem would be moving the
GRAPH
into thestatic
area: since you allocate it in themain
, it's going to be only one instance of it anyway, so declaring it static should fix the problem:static GRAPH g;
You might also want to allocate it in the dynamic area using
malloc
, but in this case it probably does not matter.这篇关于前主段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!