I have a piece of performance critical code written with pointers and dynamic memory.
I would like to rewrite it with STL containers, but I’m a bit concerned with performance. Is there a way to increase the size of a container without initializing the data?
For example, instead of doing
ptr = new BYTE[x];
I want to do something like
vec.insert(vec.begin(), x, 0);
However this initializes every byte to 0. Isn’t there a way to just make the vector grow?
I know about reserve() but it just allocates memory, it doesn’t change the size of the vector, and doesn’t allows me to access it until I have inserted valid data.
Thank you everyone.
vec.resize( newsize )is defined to have the same effect asif
newsize > vec.size()… but the compiler may have difficulty determining that it is growing, not shrinking, and by how much. You might try profiling with both.If you’re sure that the default initialization is taking time, explicitly eliminate it with your own constructor. (Even if the implicit constructor is fine, it’s good to show intent.)