Amir Bashir
Amir Bashir

Reputation: 1

my c implementation for adding a vertex into a graph fails

Here's my toy program for adding a vertex into graph:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
typedef  struct EdgeNode{
     int adjvex;
     struct EdgeNode *nextarc;
 }ENode, *PENode;
typedef struct VertexNode{
     char *data; 
     ENode *firstarc; 
 }VNode;

typedef  struct MyGraph{
     VNode vertices[INT_MAX];

}Graph;



/**void make_node(Graph *G, char *first){
     int i = 0;
     G.vertices[i++].data = first;
     G.vertices[i].firstarc = NULL;
     while(i--){
        printf("the node is %s\n",G.vertices[i].data);
     }                                     
}**/

int main (){                                                                                        
    size_t sz1;
    char *line = NULL;
    //char line[1024];
    char make_nodes[] = "@n";
    char make_edges[] = "@e";
    char check_edges[] = "@q";
    static int  i = 0;
    int  is_make_node = 0;
    //Graph* pGraph;
    Graph* pGraph = malloc(sizeof(Graph));
    if (pGraph == NULL){
        fprintf(stderr, "Unable to allocate memory for new node\n");
        exit(1);
    }


    while(getline(&line, &sz1, stdin) > 0){
        char cmd[3],n1[65],n2[65],dummy[2];
        int num_args;
        num_args = sscanf(line,"%3s%64s%64s%2s",cmd,n1,n2,dummy);
        if (strcmp(cmd,make_nodes) == 0){
            if(num_args != 2){
                printf("error\n");
            }else{
                pGraph->vertices[i].data = n1;
                pGraph->vertices[i].firstarc = NULL;
                printf("the node is %s\n",pGraph->vertices[0].data);
                i++;
            }
        }
    }
        printf("---");
        printf("the node is %s\n",pGraph->vertices[0].data);
        printf("the node is %s\n",pGraph->vertices[1].data);
        printf("the node is %s\n",pGraph->vertices[2].data);
     return 0;
}

Now the weird things happen, I would like to add vertex one by one, and when I run the code, it gives such results:

@n hey
the node is hey
@n jude
the node is jude
---
the node is jude
the node is jude
the node is (null)

@n (something) means add a node named something, while I the expected output should be:

@n hey
the node is hey
@n jude
the node is jude
---
the node is hey
the node is jude
the node is (null)

Anybody sees the problem? I don't know why when I read a new vertex the previous vertex was "covered" by the new vertex.

Upvotes: 0

Views: 183

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35154

With pGraph->vertices[i].data = n1; you let .data point to a (local) variable with block scope, which's life time will end once it goes out of scope. So you get undefined behaviour when you access vertices[x].data afterwards (i.e. after the loop and actually even at a second run in the loop, which invalidates local variables of the first run).

To overcome this, assign a copy of the content:

pGraph->vertices[i].data = malloc(strlen(n1)+1);
strcpy(pGraph->vertices[i].data, n1);

or in short (if available on your platform):

pGraph->vertices[i].data = strdup(n1);

Upvotes: 1

Related Questions