Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does reserving capacity incur two allocations or just one?

std::vector<T> vec;   // line #1
vec.reserve(100);     // line #2

I am wondering if line #1 triggers a small allocation (say, memory for 10 Ts), or if the first allocation happens on line #2. Does the standard say anything about that?

like image 265
fredoverflow Avatar asked Jan 23 '23 03:01

fredoverflow


1 Answers

It's implementation defined. The default-constructor for vector does not need to allocate anything, but it's permissible for an implementation to do so.

like image 67
Billy ONeal Avatar answered Jan 24 '23 18:01

Billy ONeal