c - Segmentation Fault before main -


so i've been running problem somehow code causing segmentation faults before of main runs. i've never had happen before , hardly have quarter's worth of coding experience i'm not sure if there's i'm doing wrong. compiles, @ least on computer, upon running main never reached.

context: i'm trying connect vertices , edges in adjacency matrix , use prim's algorithm build mst, that's later. built header file, contained typdef calls structures , functions. however, switched structure definitions header file because getting memory errors; hence why think there's issue 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 distance between 2 locations edge* connect(vertex* v1, vertex* v2); //connects 2 vertices , returns connecting edge graph primmatrix(graph *g); //connects vertices using prim's algorithm in adjacency matrix //void lprimconnect(vertex v); //connects vertices using prim's algorithm in adjacency list edge* findsmallestedge(vertex v, graph *g); //finds smallest edge connected v   #endif 

graph.c: contains implementations of functions

//functions  //computes distance between v1 , 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 , returns edge connects v1 , 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 v in graph g edge* findsmallestedge(vertex v, graph *g) {     printf("findsmallestedge");     edge *tempe;     int i, index;     index = v.index;      //set tempe equal first edge connected v     tempe = g->matrix[index][0];     //find smallest edge connected v     for(i = 0; < 756; i++)     {         if(g->matrix[index][i] -> distance < tempe->distance && g->list[index]->visited == 0)         {             tempe = g->matrix[index][i];         }     }     return tempe; }  //creates 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;     = 1;     x = 0;      new.list[0] = g->list[0];   //add root node mst     g->list[0]->visited = 2;     smallest = findsmallestedge(*new.list[0], g);      new.matrix[0][smallest->destination->index] = smallest;     //mst contain 756 nodes, run 755 times ensure nodes reached     while(i < 756)     {         x = 0;         smallest = findsmallestedge(*new.list[i], g);         //i = number of vertices 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: main function builds 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 txt file, build new vertex, , insert 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 , make connects between every vertex in list     for(i = 0; < 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 necessary, file i'm reading in from: cities.txt contains 756 entries total far code concerned size shouldn't 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 problem somehow code causing segmentation faults before of main runs.

usually, means data structures main tries place in automatic storage area overflow stack. in situation, looks graph suitable suspect that: has 2d array 571536 pointers, overflow stack before main gets chance start.

one solution problem moving graph static area: since allocate in main, it's going 1 instance of anyway, declaring static should fix problem:

static graph g; 

you might want allocate in dynamic area using malloc, in case not matter.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -