[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

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