Boost logo

Boost Users :

Subject: [Boost-users] [Boost Graph Library] Example fails with dynamic allocated array?
From: r89 (r8921025_at_[hidden])
Date: 2008-11-18 10:02:28


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-allocated-array--tp20561284p20561284.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net