1

It reads from file as it should, it adds fine all the vertices in the graph but after first call of AddEge() the program prints the first two printfs and crushes with -1 process returning code. It won't get into the while loop.

I did some pointer sketching on file using pen but hey, nothing wrong seems to me so far. Anybody ideas?

struct adjvertexlist
{
    int index_vertex;
    struct adjvertexlist *next;
};

struct vertexlist
{
    int index_vertex;
    struct vertexlist *next;
    struct adjvertexlist *list;
};


struct graphrep
{
    int nv;
    int ne;
    struct vertexlist *head;
};

typedef struct graphrep graph;

graph* AddVertex(graph *g, int v)
{
    struct vertexlist *new_vcell;
    new_vcell = (struct vertexlist*)malloc(sizeof(struct vertexlist));
    new_vcell->next = g->head;
    g->head = new_vcell;
    new_vcell->index_vertex = v;
    new_vcell->list = NULL;
    printf("Added %i vertex on graph\n", v);
    return g;
}

graph* AddEdge(graph *g, int v1, int v2)
{
    printf("\nAddEdge() started\n");
    printf("Edge to add from %i to %i and from %i to %i\n", v1, v2, v2, v1);
    printf("awesome characters");
    struct adjvertexlist *new_adjvcell;
    struct vertexlist *iterat_vlist;
    iterat_vlist = g->head;
    printf("text");
    while(iterat_vlist)
    {
        if(iterat_vlist->index_vertex == v1)
        {
            new_adjvcell = (struct adjvertexlist *)malloc(sizeof(struct adjvertexlist));
            new_adjvcell->index_vertex = v2;
            new_adjvcell->next = iterat_vlist->list;
            iterat_vlist->list = new_adjvcell;
            printf("Added edge from %i to %i\n", iterat_vlist->index_vertex, v2);
        }
        if(iterat_vlist->index_vertex == v2)
        {
            new_adjvcell->index_vertex = v1;
            new_adjvcell->next = iterat_vlist->list;
            iterat_vlist->list = new_adjvcell;
            printf("Added edge from %i to %i\n", iterat_vlist->index_vertex, v1);
        }
        iterat_vlist = iterat_vlist->next;
    }
    printf("AddEdge() finished\n\n");
    return g;
}
graph* ReadGraph(graph *g, FILE *f)
{
    int i, x, y;
    fscanf(f, "%i %i", &g->nv, &g->ne);
    printf("Number of vertices read: %i\n", g->nv);
    printf("Number of edges read: %i\n", g->ne);
    for(i=1; i<=g->nv; i++)
    {
        fscanf(f, "%i", &x);
        g = AddVertex(g, x);
    }
    for(i=1; i<=g->ne; i++)
    {
        fscanf(f, "%i %i", &x, &y);
        printf("Edge read: %i, %i\n", x, y);
        g = AddEdge(g, x, y);
    }
    fclose(f);
    return g;
}

The main function

int main()
{
    graph *g1;
    FILE *f1;
    g1 = (graph *)malloc(sizeof(graph));
    g1->head = NULL;
    f1 = fopen("graph1.txt", "r");
    g1 = ReadGraph(g1, f1);
    return 0;
}

The content of graph1. txt file

4 3
9 17 103 5002
9 17
17 103
103 5002
floreapaun
  • 124
  • 10
  • You shouldn't really [cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) in C. And for problems like these, you should [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Apr 22 '18 at 14:57
  • @HenriqueJung I agree with you, but the better way of asking for helpful code is referring to the concept of a [mcve]. – Yunnosch Apr 22 '18 at 16:39

1 Answers1

1

On the AddEdge function, you have two if statements

    if(iterat_vlist->index_vertex == v1)
    {
        new_adjvcell = (struct adjvertexlist *)malloc(sizeof(struct adjvertexlist));
        new_adjvcell->index_vertex = v2;
        new_adjvcell->next = iterat_vlist->list;
        iterat_vlist->list = new_adjvcell;
        printf("Added edge from %i to %i\n", iterat_vlist->index_vertex, v2);
    }
    if(iterat_vlist->index_vertex == v2)
    {
        new_adjvcell->index_vertex = v1;
        new_adjvcell->next = iterat_vlist->list;
        iterat_vlist->list = new_adjvcell;
        printf("Added edge from %i to %i\n", iterat_vlist->index_vertex, v1);
    }

In this context new_adjvcell has garbage data, because it's never initialised. If iterat_vlist->index_vertex == v1 then everything works correctly because you called malloc on it, however if index_vertex == v2 you use new_adjvcell it without calling malloc on it first, like the previous if. I found it using a debugger, which promptly showed me the crashing line.

I didn't check if the algorithm is correct though.

Henrique Jung
  • 1,408
  • 1
  • 15
  • 23
  • It works as expected adding that malloc in the second if, I don't know why I was omitting it. But why the function crushes before entering the while loop? "awesome characters" and "text" don't get printed. – floreapaun Apr 23 '18 at 04:06
  • @lafleur Every outputs are buffered, and it may not flush the buffer before crashing. – Phantom Apr 23 '18 at 06:11
  • @lafleur I only have this crash if `graph1.txt` does not exist. Maybe you should capture the error from `fopen`. – Henrique Jung Apr 23 '18 at 13:10