adjacency_list going out of memory range?
Hi, I'm working with GNU/Linux, Debian Sarge, libboost-graph1.28.0, gcc version 2.95.4 20011002 (Debian prerelease). I'm quite new to this, so I don't know if I'll be able to explain my problem correctly. It seems to me as if when I declare a graph, it writes on memory already used by another data structure. The parts of the code that I consider relevant are (.hpp and .cpp merged) ------------------- | | \|/ ------------------- #include <utility> // for std::pair #include <boost/graph/adjacency_list.hpp>/* for namespace boost * vertex_index_t * edge_weight_t * listS * undirectedS */ using namespace boost; namespace boost { enum vertex_to_be_spanned_t { // used to mark vertices of W vertex_to_be_spanned }; BOOST_INSTALL_PROPERTY(vertex, to_be_spanned); } namespace steiner { typedef std::pair<int,int> Edge; // type of edges /* Steiner Problem Graph: * - multigraph * - add_edge(), ... stability and speed, bad space overhead * - vertex indices, edge weights */ typedef property <vertex_index_t, int, property <vertex_to_be_spanned_t, bool> > VertexProperty; typedef property <edge_weight_t, int> EdgeProperty; typedef adjacency_list <listS, listS, undirectedS, VertexProperty, EdgeProperty > SteinerGraph; } namespace steiner { #define EX_FORMAT "not a valid steiner problem file"; } #include <iostream> // for std::cout #include <fstream> // for std::ifstream using namespace steiner; namespace steiner { int readsteinerORfile(steiner::SteinerGraph& g, char *s); } namespace steiner { inline void skipspaces(std::ifstream& fp, char& c); } namespace steiner { inline int readchar2int(std::ifstream& fp, char& c); } /* int main(int argc, char *argv[]) * -------------------------------- * *argv[] (IN): file names array * -------------------------------- * return n: n files were succesfully read, n > 0 * return 0: no files could be read * return -1: unexpected error * -------------------------------- * read steiner problem from files */ int main(int argc, char *argv[]) { ... SteinerGraph g; nf = steiner::readsteinerORfile(g, argv[i]); ... } /* int readsteinerORfile(VertexListGraph& g, char *s) * -------------------------------- * g (OUT): graph read from file * *s (IN): file name * -------------------------------- * return 1: file successfully read * return 0: file could not be read * return -1: unexpected error * -------------------------------- * read steiner problem from one file */ namespace steiner { int readsteinerORfile(steiner::SteinerGraph& g, char *s) { ... int num_edg=0, num_ver=0, // number of edges, vertices num_verw=0; // number of non-steiner vertices char c; // file reader int i; // counter int x; // temp value int *weight_array=NULL; // edge weights read from file Edge *edge_array=NULL; // array of edges read from file int *w_array=NULL; // vertices of W ... weight_array = new int[num_edg]; if (weight_array == NULL) throw (char *)EX_MEM; edge_array = new Edge[num_edg]; if (edge_array == NULL) throw (char *)EX_MEM; w_array = new int[num_verw]; if (w_array == NULL) throw (char *)EX_MEM; ... *************** HERE ******************* /* creation of graph object */ g.clear(); g = SteinerGraph(num_ver); *************** HERE ******************* ... } } ------------------- | | \|/ ------------------- And what happens? If I print the contents of w_array[] at the first "HERE", I get Puf(W(0)=0 )! Puf(W(1)=1 )! Puf(W(2)=2 )! Puf(W(3)=3 )! but if I print them again at the second "HERE", I get Puf(W(0)=8 )! Puf(W(1)=1 )! Puf(W(2)=2 )! Puf(W(3)=-15 )! Does anybody knows what could be happening? Thank you. Regards, Ramón Casero.
Hi Ramón, Could you post complete (compilable) code so that I can easily reproduce the problem. Thanks, Jeremy
Jeremy Siek wrote:
Hi Ramón,
Could you post complete (compilable) code so that I can easily reproduce the problem.
Oops, I made a silly mistake, I feel embarrassed, and hope you did not lose time with my message. Altough I am new to C++ and BGL, I have some experience with C, I should have not made this error. I just allocated memory for the overwritten array in the wrong place of the code, so the array was shorter than I thought it was. I realized when linking with electric fence. I'm sorry again, now it works fine Ramón.
participants (2)
-
Jeremy Siek
-
Ramón Casero Cañas