GCC STL implementation bug?

Paolo Carlini pcarlini@unitus.it
Wed Sep 24 03:56:00 GMT 2003


Not a bug.
You have to call vec_in.reserve(2*vec_in.size()); before your copy().
See, f.i., this section in the Errata of Josuttis book:

*Page 273, Section 7.4.2
*In iter/backins.cpp there is a nasty problem with the following statement:

  copy (coll.begin(), coll.end(),   /// source /
        back_inserter(coll));       /// destination/

Because the back inserter inserts elements into a vector, these 
insertions might invalidate all other iterators referring to the same 
vector. Thus, the algorithm invalidates the passed source iterators 
while running. To avoid this behavior, you have to reserve enough space 
so that the reallocation won't happen. So, insert before the statement 
above:

  /// reserve enough memory to avoid reallocation/
  coll.reserve (2*coll.size());

Paolo.



More information about the Gcc-bugs mailing list