Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how does reserve() work?

Reply
Thread Tools

how does reserve() work?

 
 
JDT
Guest
Posts: n/a
 
      04-13-2007
Hi,

My understanding about vector's reserve() is to allocate memory for the
vector. If so, is it right that each push_back() in the following loop
causes no memory reallocation and its execution time should be constant
(i.e. internally, only one value is copied and then the integer "size"
is increased by one)? Thanks for any help.

vector<int> v;
v.reserve(100);
for (int i=0; i<100; i++)
v.push_back(i);

JD
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-13-2007
JDT wrote:
> My understanding about vector's reserve() is to allocate memory for
> the vector. If so, is it right that each push_back() in the
> following loop causes no memory reallocation and its execution time
> should be constant (i.e. internally, only one value is copied and
> then the integer "size" is increased by one)? Thanks for any help.


Yes. The value when it's pushed without reallocation is simply
copy-constructed using "placement new" (in most implementations).

Most implemenations employ some kind of "hidden" reserve() even
though you don't use it directly. They usually allocate more memory
than is immediately needed to prevent frequent reallocations. To
learn the capacity of the vector (how many elements it can contain
before next reallocation), call 'capacity' member function.

> vector<int> v;
> v.reserve(100);
> for (int i=0; i<100; i++)
> v.push_back(i);


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
haijin.biz@gmail.com
Guest
Posts: n/a
 
      04-14-2007
your understanding is perfect --- as it is the same as one of my c++
books said.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-14-2007
On Apr 14, 1:24 am, JDT <(E-Mail Removed)> wrote:

> My understanding about vector's reserve() is to allocate memory for the
> vector. If so, is it right that each push_back() in the following loop
> causes no memory reallocation and its execution time should be constant
> (i.e. internally, only one value is copied and then the integer "size"
> is increased by one)? Thanks for any help.


> vector<int> v;
> v.reserve(100);
> for (int i=0; i<100; i++)
> v.push_back(i);


That's correct. More importantly, it is guaranteed that none of
the push_back's invalidate iterators, references or pointers
into the vector, e.g.:

vector< int > v ;
v.push_back( 0 ) ;
vector< int >::iterator i = v.begin() ;
int& r = v[ 0 ] ;
int* p = &v[ 0 ] ;
for ( int i = 1 ; i < 100 ; ++ i ) {
v.push_back( i ) ;
}

With the reserve, i, r and p are guaranteed to still be valid
here. Without the reserve, no.

In most programs, this is by far the real reason to use reserve.
The standard requires amortized constant time for push_back
anyway, so you won't get very many re-allocations, regardless of
what you do (and, roughly speaking, pushing back 10000 elements
will take 10 times longer, and no more, than pushing back 1000,
and 100 times more than pushing back 100). On the other hand,
an invalid iterator or pointer can be a real pain.

--
James Kanze (Gabi Software) email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments