main 之前的分段错误 [英] Segmentation Fault before main

查看:15
本文介绍了main 之前的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一个问题,在我的任何主要实际运行之前,我的代码以某种方式导致分段错误.我以前从未发生过这种情况,而且我几乎没有四分之一的编码经验,所以我不确定我是否做错了什么.一切都可以编译,至少在我的电脑上是这样,但在运行它时,我的主程序永远不会到达.

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.

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

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:包含我所有函数的实现

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:我构建图形的主要函数

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
");
        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, "%[^	]	%[^	]	%s", city, long1, lat1); 
        printf("scanned in data
");
        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;
}

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

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.

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

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 the GRAPH is a suitable suspect to do just that: it has a 2D array with 571536 pointers, which could very well overflow the stack before your main gets a chance to start.

这个问题的一个解决方案是将 GRAPH 移动到 static 区域:因为你在 main 中分配它,它会无论如何都只是它的一个实例,因此将其声明为静态应该可以解决问题:

One solution to this problem would be moving the GRAPH into the static area: since you allocate it in the main, it's going to be only one instance of it anyway, so declaring it static should fix the problem:

static GRAPH g;

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

You might also want to allocate it in the dynamic area using malloc, but in this case it probably does not matter.

这篇关于main 之前的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆