It is standard to use RAII. Having an exception be thrown in the face of a single call to new in such a case is safe: the exception will just be propagated. You need the try block because you need to delete if an exception is thrown later on, but with RAII that worry is eliminated.Originally Posted by ~Kyo~
Sure, but for all your talk about avoiding overhead, you described a solution that has a great deal more overhead than what I proposed in terms of the invocations of new[] in the loop, compared to say, two invocations of new[].Originally Posted by ~Kyo~
That may well be fine. The complexity of push_back is amortized O(1) because of how the vector's capacity grows (i.e., by a factor, not a constant).Originally Posted by ~Kyo~
You could define a larger bounds on a vector<datatype here> and have the same flexibility, as well as the same possible space wastage.Originally Posted by ~Kyo~
The vector uses the provided allocator, which may or may not use the new[] that you have in mind.Originally Posted by ~Kyo~
If saving the space that would have been used to store the capacity really was your point, then it is a non-issue: you could use the up and coming std::unique_ptr with a custom deleter, or implement your own non-expandable vector class template (extremely easy since it is err... not expandable).Originally Posted by ~Kyo~