views:

392

answers:

1

Hi,

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

+3  A: 

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.

sbk
Thanks a lot, it work, and it is nice because I learned something that I thought I new.
marouanebj
Funny thing is, I didn't really know about `reverse_iterator::base` (who uses reverse iterators anyway? :) I looked at your code, thought "base(), wtf?" looked it up, and the problem became apparent. I learned something today too
sbk