This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC STL implementation bug?


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]