stl – c++ std::vector Orphan Range error – Education Career Blog

A program dealing with graphs(from graph theory) representation and transformation.The adjacency list and matrix are implemented like dynamic arrays of vectors(don’t ask why not vector of vector) for the following function program exits with memory error and compiler pointing to the orphan vector definition.

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=Listin.begin();j!=Listin.end();j++){
   for(int k=0;k<vertexNumber;++k){
    if(k==*j) Matrixcont.push_back(1); 
    else Matrixcont.push_back(0);
   }
   cont++;
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>vertex;
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);

The “source of error” in STL where compiler points:

http://i51.tinypic.com/2dt0t9e.jpg


The error message:

Unhandled exception at 0x001a543b in graph.exe: 0xC0000005: Access violation reading location 0xfdfdfe01.


the fillList function used to fill the adjacency list :

void fillList(int vertexNumber, vector<int> *List){
    int input=0;
    for (int i=0;i<vertexNumber;i++){   
        int in=i;
        cout<<"Introduce adjacent vertexes for the vertex -"<<i+1<<"-:"<<endl;
        for(int j=0;j<vertexNumber;j++){
            std::cout<<i+1<<"-";
            std::cin>>input;
            if(input==0) break;
            Listi.push_back(input-1);
        }
    }
}

Any clue is welcome.


,

One problem is that you haven’t initialized adjacencyList yet. According to this code, it should be pointing at garbage (or null) when it gets into listToAdMatrix. That sould cause an error when trying to index into Listin.

,

Are you sure that adjacencyListin.size() is always less than vertex? Put code before the j loop to check for that. Right now you just need to put a dummy line and a break point to see if it can happen.

If it does, then either adjacencyList is initialized incorrectly or this is a situation that you need to handle.

You also need to make sure that adjacencyList is set to an array of length vertex, but that’s more obvious, so you are probably doing that.

,

Problem Solved :

Once again i’ve read outside of an array.The error was in the cont variable which i used to address the vectors in the Matrix(dynamic array), incrementing it while it got outside the array.Problem is solved by inserting a while statement.Thank you all for the answers.

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=Listin.begin();j!=Listin.end();j++){
   while(cont!=vertexNumber){
     for(int k=0;k<vertexNumber;++k){
       if(k==*j) Matrixcont.push_back(1); 
       else Matrixcont.push_back(0);
     }
     cont++;
   }
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>vertex;
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);

Leave a Comment