0

it's my first question here so I apologize for eventual formal mistakes you may found in my post.

I'm coding a simple class for "Undirected Connected Weighted Graphs", which must use Adjacency Lists based on vectors.

The issue is that when I run the program from Eclipse, MS Windows says it "stops working" and after debugging I get an "Unhandled exception at 0x00AE251A .... Access violation writing location..." message. Looking around I found that this issue may be caused by a missing pointer destruction or pointer initialization (?). I switched from the standard pointer to the shared_ptr to troubleshoot this issue but the error is the same...

Can anybody enlighten me on this? I lost almost a whole day trying to find the cause without success.

class UndirectedGraph
{
private:
    int V;                                  
    std::vector<std::shared_ptr<std::pair<int,int>>>* adj;    
public:
    UndirectedGraph(int V)
{
        this->V = V;
        this->adj = new std::vector<std::shared_ptr<std::pair<int,int>>>;
}

void addEdge(int v, int w, int weight)
{
    auto sp = std::make_shared<std::pair<int,int>>(std::make_pair(v,weight));
    adj[v].push_back(sp);
}

int main()
{
    UndirectedGraph G1(7);//Ok
    G1.addEdge(0,1,9);//Ok
    G1.addEdge(1,2,5);//Ok
    G1.addEdge(2,0,8);//EXCEPTION RAISED HERE (if line is commented all run fine)
    return 0;
}
mitxael
  • 52
  • 7
  • 1
    You are not making use of the V parameter in your constructor so your vector this->adj is empty when you try to `adj[v].push_back(sp);` – drescherjm Apr 30 '15 at 01:15
  • The problem persists even if I add (V) as size at the end of the "adj" inizialitation. Also, I think push_back should work even if vector is empty, right? – mitxael Apr 30 '15 at 01:23

1 Answers1

1

I noticed a couple of errors in the code:

  1. If what you need are adjacency lists, then this->adj should be a vector of vectors. Currently, its just a 1-D vector of <int,int> pairs. Instead it should be:

    std::vector<std::vector<std::shared_ptr<std::pair<int,int>>>>* adj;

  2. In the constructor, this->adj should be initialized as follows:

    this->adj = new std::vector<std::vector<std::shared_ptr<std::pair<int,int>>>>(V);

  3. Now, in the addEdge function, you need to first access the vector corresponding to node 'v' and then, into that vector, you need to push the pair (w, weight) [NOTE that, even if we ignore the error that there's only vector, the logic is still incorrect since you're pushing (v, weight) instead of (w, weight) into that vector]. The modified addEdge function would be something like this:

    void addEdge(int v, int w, int weight)
    {
        auto adjacencyList = adj->at(v);
        auto sp = std::make_shared<std::pair<int,int>>(std::make_pair(w,weight));
        adjacencyList.push_back(sp);
    }
    

Hope this helps you

jithinpt
  • 1,204
  • 2
  • 16
  • 33
  • Thanks for your answer @jithinpt! Well-structured and good explanation. I'm new at C++ and I got a little confused. Could you tell me the advantage of defining a 2D LinkedList using a pointer like this: `std::list* adj= new std::list[V];` rather than simply doing this: `std::list adj= new std::list[V];` Thank you! – mitxael Apr 30 '15 at 13:01
  • 1
    @mitxael: Only the first one is valid. If you try the second approach, you'll get a compiler error. To briefly clarify why this is the case: There are two ways of allocating any object (list, vectory, etc.). You can either allocate it on the stack or on the heap. When you create an object using the 'new' keyword, the object is allocated space on the heap and you are provided with a 'pointer' to that space. This is the reason you have to use a '*' when defining the return type (eg: list * adj). To allocate on the stack, you should merely declare it as std::list adj. – jithinpt Apr 30 '15 at 17:54
  • 1
    The previous reply was very brief due to the limit on the max number of characters. This SO post might help clarify your doubts further: http://stackoverflow.com/questions/8036474/allocating-vectors-memory-on-heap-or-stack – jithinpt Apr 30 '15 at 17:56
  • 1
    Another post that may be helpful: http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c – jithinpt Apr 30 '15 at 18:04