简体   繁体   中英

vector iterator not dereferencable at runtime on a vector<vector<vector<A*>*>*>

I have this destructor that create error at runtime "vector iterator not dereferencable".

The gridMatrix is a std::vector<std::vector<std::vector<AtomsCell< Atom<T> * > * > * > * >

I added the typename and also the typedef but I still have the error.

I will move for this idea of vect of vect* of vect* to use boost::multi_array I think, but still I want to understand were this is wrong.

/// @brief destructor
~AtomsGrid(void) {
  // free all the memory for all the pointers inside gridMatrix (all except the  Atom<T>* )
  //typedef typename ::value_type value_type;

  typedef std::vector<AtomsCell< Atom<T>* >*> std_vectorOfAtomsCell;
  typedef std::vector<std_vectorOfAtomsCell*> std_vectorOfVectorOfAtomsCell;

  std_vectorOfAtomsCell* vectorOfAtomsCell;
  std_vectorOfVectorOfAtomsCell* vectorOfVecOfAtomsCell;
  typename std_vectorOfVectorOfAtomsCell::iterator itSecond;
  typename std_vectorOfVectorOfAtomsCell::reverse_iterator reverseItSecond;
  typename std::vector<std_vectorOfVectorOfAtomsCell*>::iterator itFirst;


  //typename std::vector<AtomsCell< Atom<T>* >*>* vectorOfAtomsCell;
  //typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>* vectorOfVecOfAtomsCell;
  //typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>::iterator itSecond;
  //typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>::reverse_iterator reverseItSecond;
  //typename std::vector<std::vector<std::vector<AtomsCell< Atom<T>* >*>*>*>::iterator itFirst;


  for (itFirst = gridMatrix.begin(); itFirst != gridMatrix.end(); ++itFirst) {
    vectorOfVecOfAtomsCell = (*itFirst);
    while (!vectorOfVecOfAtomsCell->empty()) {
      reverseItSecond = vectorOfVecOfAtomsCell->rbegin();
      itSecond = vectorOfVecOfAtomsCell->rbegin().base();

      vectorOfAtomsCell = (*itSecond); // ERROR during run: "vector iterator not dereferencable"
      // I think the ERROR is because I need some typedef typename or template ???!!!
      // the error seems here event at itFirst

      //fr_Myit_Utils::vectorElementDeleter(*vectorOfAtomsCell);
      //vectorOfVecOfAtomsCell->pop_back();
    }
  }
  fr_Myit_Utils::vectorElementDeleter(gridMatrix);
}

If someone want the full code that create the error I'm happy to give it but I do not think we can attach file in the forum. BUT still its is not very big so if you want it I can copy past it here.

Thanks

If v is a std::vector then v.rbegin().base() == v.end() is true. In your code, itSecond is actually equal to vectorOfVecOfAtomsCell.end() , which is beyond the vector end and not dereferencable. See the MSDN page for reverse_iterator::base for more information.

As to solutions: in the code you posted, I don't really see why you need itSecond at all. vectorOfAtomsCell = *reverseItSecond should yield the result you are expecting, without the error. If you really need an iterator to the last element you should either do itSecond = vectorOfVecOfAtomsCell->end()-1 or itSecond = reverseItSecond.base()-1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM