6

I'm trying to use the boost graph library, and I'm getting a segfault when I try to use boost::edge(). The full code is available here, but here I've made a minimal program that has the same issue (I'm compiling 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 add_edge, remove_edge lines, The 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 hackery? Thanks!

bgschiller
  • 2,087
  • 1
  • 16
  • 30
  • This looks a lot like a bug. You might want to bring it up on the [Boost-Devel Mailing List](http://news.gmane.org/gmane.comp.lib.boost.devel) if you can't get a proper response on Stackoverflow. – Mankarse Mar 15 '13 at 02:34

1 Answers1

1

Apparently, the add_edge(1,3,G) call adds vertices to the graph if needed. Your first call is in that case. Then it adds the 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.

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

The call to edge(1,3,G) on the other hand does not add any vertex to the graph, the boolean in the return is there to state if vertices 1 and 3 are connected or not. There is an access violation is you remove the add_edge because the vertices at index 1 and 3 do not exist.

You can simply initialize the graph with the desired number of vertices:

graph_t G(4);
Raffi
  • 3,068
  • 31
  • 33
  • I appreciate the attention, but this was posted 6 months ago. I'm no longer actively working on this project. Also, it's not clear from the example above, but I think that it must be a bit more complicated than you're describing. I don't think calling add_edge(1,3,G) will add vertices 0 and 4. – bgschiller Sep 16 '13 at 19:21
  • 1
    Actually, I traced the instruction with the debugger, and was surprised myself to find "if (x >= num_vertices(g_)) g_.m_vertices.resize(x + 1);" (boost\graph\detail\adjacency_list.hpp, line 2186, boost 1.50) – Raffi Sep 16 '13 at 19:25
  • Anyway, any question of this kind deserves an answer. – Raffi Sep 16 '13 at 19:26
  • 1
    Hmm, I guess you're right. I was hesitant to accept your answer because it requires initializing the graph, but it sounds like even that happens implicitly anyway. Well done finding that! – bgschiller Sep 17 '13 at 03:23
  • The implementation seems strange to me. I would expect it to return .second=false if either of the vertices of the requested edge did not exist. – David Doria May 02 '14 at 20:27