push back - Vector does reallocation on every push_back -


ide - visual studio 2008, visual c++

  1. i have custom class class1 copy constructor it.
  2. i have vector
  3. 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

  1. is mandatory use reserve statement?
  2. 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

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -