push back - Vector does reallocation on every push_back -
ide - visual studio 2008, visual c++
- i have custom class class1 copy constructor it.
- i have vector
- data inserted using following code
class1* objclass1; vector<class1> vclass1; for(int i=0;i<1000;i++) { objclass1 = new class1(); vclass1.push_back(*objclass1); delete objclass1;
}
now on every insert, vector gets re-allocated , existing contents copied new locations. example, if vector has 5 elements , if insert 6th one, previous 5 elements along new 1 gets copied new location (i figured out adding log statements in copy constructors.)
on using reserve(), not happen expected! have following questions
- is mandatory use reserve statement?
- does vector reallocation every time push_back; or happen because debugging?
find out putting copy constructor test non debug code, , let know platform! imo vector shouldn't reallocate on every pushback. there smarter ways manage memory, , i'd bet money implementers didn't that.
Comments
Post a Comment