Boost :: edge calling segfault

I try to use the boost graph library and I get segfault when I try to use boost :: edge (). The full code is available here , but here I made a minimal program that has the same problem (I compile with "g ++ minimal.cpp"):

#include<stdio.h> #include<boost/graph/adjacency_list.hpp> using namespace boost; using namespace std; typedef adjacency_list<> graph_t; typedef graph_traits<graph_t>::edge_descriptor edge_descriptor; int main(){ graph_t G; //add_edge(1,3,G); //remove_edge(1,3,G); pair<edge_descriptor, bool> res = edge(1,3,G); printf("G does %shave an edge 1->3\n", res.second ? "" : "not "); return 0; } 

If I uncomment the lines add_edge, delete_edge, Segfault does not occur, and the program prints the expected

 G does not have an edge 1->3 

but is there a way to avoid such a hacker? Thanks!

+6
source share
1 answer

Apparently, calling add_edge(1,3,G) adds vertices to the chart if necessary. Your first call in this case. Then he adds an edge from vertex 1 to vertex 3. Note that after this call, the number of vertices is 4, since the vertices are then indexed from 0 to 3.

A subsequent call to remove_edge(1,3,G) removes the edge just added, but leaves the number of vertices unchanged.

On the other hand, calling edge(1,3,G) does not add any vertices to the graph; the boolean in the return should indicate whether vertices 1 and 3 are connected or not. There is an access violation, you remove add_edge because the vertices in indexes 1 and 3 do not exist.

You can simply initialize the chart with the required number of vertices:

 graph_t G(4); 
0
source

All Articles