[Boost Graph Library] Example fails with dynamic allocated array?

Hi all, The following code is from http://www.boost.org/doc/libs/1_37_0/libs/graph/example/dijkstra-example.cpp and it works fine on my computer. However, if I change int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; to int *weights = new int[9]; and, similarly, change the edge_array to Edge *edge_array = new Edge[9]; (followed by assigning the weights and edges of course), then the results are different than the original code. Can anyone see why they are different? The reason I need to dynamically allocate, is because I don't know the number of nodes and edges at compile time. Please provide suggestions, thanks a lot! (Code below comes from http://www.boost.org/doc/libs/1_37_0/libs/graph/example/dijkstra-example.cpp) typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, int > > graph_t; typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; typedef std::pair<int, int> Edge; const int num_nodes = 5; enum nodes { A, B, C, D, E }; char name[] = "ABCDE"; Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E), Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) }; int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; int num_arcs = sizeof(edge_array) / sizeof(Edge); graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g); std::vector<vertex_descriptor> p(num_vertices(g)); std::vector<int> d(num_vertices(g)); vertex_descriptor s = vertex(A, g); dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); -- View this message in context: http://www.nabble.com/-Boost-Graph-Library--Example-fails-with-dynamic-alloc... Sent from the Boost - Users mailing list archive at Nabble.com.

r89 wrote:
Can anyone see why they are different? ... int num_arcs = sizeof(edge_array) / sizeof(Edge);
The above line will not work correctly when edge_array is a pointer instead of a c-array.

Can anyone see why they are different? The reason I need to dynamically allocate, is because I don't know the number of nodes and edges at compile time. Please provide suggestions, thanks a lot!
Define "different". You don't need to worry about any of the allocation. Just use add_vertex() and add_edge() as need to insert data into your graph. The graph implementation will manage all of the allocation for you. Andrew Sutton andrew.n.sutton@gmail.com
participants (3)
-
Andrew Sutton
-
r89
-
Thomas Klimpel