Topic: BUG In stl Vector erase


Author: Andrew Gierth <andrew@erlenstar.demon.co.uk>
Date: 1998/03/15
Raw View
>>>>> "Ruard-dumaine" == Ruard-dumaine sylvain <sruard-dumaine@eden-studios.fr>
 writes:

 Ruard-dumaine> I think we have found a bug in the erase() method of
 Ruard-dumaine> vectors.

 Ruard-dumaine> We are using vectors of a class witch does dynamic
 Ruard-dumaine> allocation in the constructor and free in the
 Ruard-dumaine> destructor. When we erase an element in the middle of
 Ruard-dumaine> the vector, the last element of the vector becomes
 Ruard-dumaine> invalid.

This suggests a missing or broken copy-assignment operator, not a bug
in the library.

--
Andrew.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ruard-dumaine sylvain <sruard-dumaine@eden-studios.fr>
Date: 1998/03/11
Raw View
I think we have found a bug in the erase() method of vectors.

We are using vectors of a class witch does dynamic allocation in
the constructor and free in the destructor. When we erase an element
in the middle of the vector, the last element of the vector becomes
invalid.

After the copy operation, the last and last-1 element of the vector
have the same value, including the pointer to the allocated area.
Calling 'destroy' on 'finish' frees the memory block, and invalidate
the pointer for both last and last-1 witch is still in use, whereas
the memory allocated by the real removed element is still allocated,
but no longer referenced.

The bug fix should be something like:

iterator erase(iterator position) {
   destroy(position);  // Call the destructor of the element to be
removed
   if (position + 1 != end())
     copy(position + 1, finish, position); // Copy remaining elements
   --finish;
   return position;
}




 -------------------------------------------
|            EDEN STUDIOS                   |
|           R&D Department                  |
| MA  Argenton &  S Ruard-Dumaine           |
| E-mail  : sruard-dumaine@eden-studios.fr  |
          : maargenton@eden-studios.fr      |
 -------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]