1,105,232 Community Members

Graph theory implementation problem

Member Avatar
tahsin.rahit
Newbie Poster
11 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

This code is giving me runtime error. Here, first I want to take a integer input which determine the number (n) of nodes in the graph. Then for n-1 time I want take two integer input which will give the adjacency information.

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

#define MAXEDGE 1000


using namespace std;


typedef struct{
    int linked_to;
}edg;

typedef struct{
    edg edge[MAXEDGE];
    bool visited[MAXEDGE];
    int total_nodes;
}graph;
	
int main() {
    
        int u,v,n = 0;
        graph *g;
        
        scanf("%d",&g->total_nodes);
       
        for(int i = 0; i<g->total_nodes; i++){
                scanf("%d %d",&u,&v);
                g->edge[u].linked_to = v;
        }        
    
    return 0;
}
Member Avatar
Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: -8 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Where did you allocate the memory

Member Avatar
tahsin.rahit
Newbie Poster
11 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Where did you allocate the memory

Sorry, I don't understand your question. Why I allocate memory here?

Member Avatar
Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: -8 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

g is pointer to an object of type graph,
you have to create the object, by either malloc or new
and then point g to the address

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
1
 

graph *g; creates a pointer (to memory) but you fail to provide any memory to point to. You can do one of three things:

  • Create an object instead of a pointer ( graph g; )
  • Create an object and assign the pointer to that objects address ( graph g, *gp = &g; )
  • Allocate the memory yourself ( graph * g = new graph; )
Member Avatar
tahsin.rahit
Newbie Poster
11 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

graph *g; creates a pointer (to memory) but you fail to provide any memory to point to. You can do one of three things:

  • Create an object instead of a pointer ( graph g; )
  • Create an object and assign the pointer to that objects address ( graph g, *gp = &g; )
  • Allocate the memory yourself ( graph * g = new graph; )

Thank You. 2nd one is working for me. But what about 1st one? How can I assign value for the object(g) using 1st option?

Would you plz make me clear about how 2nd one is working? Is it like this...
For example, pointer gp is first pointing the object g which is a graph type object. then the value of total_nodes is assigned through the pointer to the object.

Member Avatar
mike_2000_17
21st Century Viking
4,062 posts since Jul 2010
Reputation Points: 2,244 [?]
Q&As Helped to Solve: 795 [?]
Skill Endorsements: 71 [?]
Moderator
Featured
Sponsor
 
0
 

When you have syntax like g->total_nodes it means that you access the object pointed to by "g" and then access its data member called total_nodes .

When you write it as g.total_nodes it means that you access the data member called total_nodes that belongs to the object "g".

So, if you want the first version to work, i.e. the version with Graph g; , then you have to replace all the g-> with g. , because now, "g" is an object, and not a pointer to an object (like in your original code).

Question Answered as of 2 Years Ago by Ali_2101, mike_2000_17 and L7Sqr
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: