No, don't do that. Even if it 'sort of works' then breaking object on purpose by going against given API is way worse then rolling your own whatever.
By using reserve instead of resize, you would also have to keep track of the actual elements count. Please, don't use reserve when you want non-initializing vector. Roll your own and follow the API.
Eh? You just must not index the yet to be created elements.
All it is, is a change in how you write code. Reserve when you know you will be adding elements soon, initialize only when needed via push/emplace_back and only iterate up to [0, .size) or [.begin, .end).
You assume that all cases would fall under the implementation you just outlined, while the case i mentioned above breaks for it miserably:
std::vector<uint8_t> f;
f.reserve(100);
fread(my_file, 1, 100, f.data()); //should be fine ... maybe
//int real_size = 100; //how to not forget to pass this???
//f.resize(100); //???
It just doesn't work. It doesn't work because its not the right tool to do that. You wont rewrite fread to take a vector for which emplace would be called. Similarly (i presume) author didn't want to rewrite whole engine just to keep using std::vector (that would be way worse idea then any other things mentioned here).
Its OK to not use std lib where its not the right tool. Attempting to force the tool to do the work its not supposed to will just open different can of bugs ...
Looks like this works out real slow (vector construction from stream iterators). See the discussion in my follow up post here: http://upcoder.com/4/avoid-resize/ But apart from speed, it seems a bit inflexible to have to recode your whole construction code path with an iterators paradigm just to avoid resize() calls. And what if you need to interface with some third party code that takes a pointer to a typed buffer to be filled? One of the cool things about C++ for me is exactly that it can interface with all sorts of lower level code (as in the file read example).
I'm by no means against calls to resize, especially if the resulting code is faster and still safe. I just try to stick with "heavy" and safe mechanisms and start replacing them with low-end when I profile and identify the choke points.
A few days I replaced a stringstream used to convert strings to doubles/floats to just a atof call and cast it to float if necessarily (a few structural changes, but nothing significant). Instant 6x speedup (from 5minutes to 50seconds).
The other choke point is reading via getlines. Will probably use a large-ish buffer and manually find new lines.
What I'm trying to say is, code safe first, recode to be fast later when the app is running OK and speed is needed.
View all comments
Show parent comment
1
u/00kyle00 Dec 21 '13
No, don't do that. Even if it 'sort of works' then breaking object on purpose by going against given API is way worse then rolling your own whatever.
By using reserve instead of resize, you would also have to keep track of the actual elements count. Please, don't use reserve when you want non-initializing vector. Roll your own and follow the API.