Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector: reserve required?

Reply
Thread Tools

std::vector: reserve required?

 
 
Mike -- Email Ignored
Guest
Posts: n/a
 
      07-04-2008
In std::vector, is reserve or resize required?

On:
Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU/Linux
Using:
g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)

The program below fails, but if the reserve(en)
is uncommented, it works. Is this as expected?

// vectst.cc 07/04/08

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char* argv[])
{
int en = 10;
vector<int> vec;

// vec.reserve(en);
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;

exit (0);
}

Thanks,
Mike.
 
Reply With Quote
 
 
 
 
Darío Griffo
Guest
Posts: n/a
 
      07-04-2008

Mike -- Email Ignored wrote:
> In std::vector, is reserve or resize required?
>
> On:
> Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
> Jul 27 18:10:34 EDT 2007 i686 athlon
> i386 GNU/Linux
> Using:
> g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
>
> The program below fails, but if the reserve(en)
> is uncommented, it works. Is this as expected?
>
> // vectst.cc 07/04/08
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> int main(int argc, const char* argv[])
> {
> int en = 10;
> vector<int> vec;
>
> // vec.reserve(en);
> for (int jj = 0; jj < en; ++jj)
> vec[jj] = jj;
> for (int jj = 0; jj < en; ++jj)
> cout << vec[jj] << endl;
>
> exit (0);
> }
>
> Thanks,
> Mike.



No, you could do that (use reserve), or, use push_back()

int main(int argc, const char* argv[])
{
int en = 10;
vector<int> vec;

for (int jj = 0; jj < en; ++jj)
vec.push_back(jj);
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;

return 0;
}
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-04-2008
Mike -- Email Ignored wrote:

> In std::vector, is reserve or resize required?


No, but you need to fill the vector somehow. You can do that either upon
construction, or by using push_back, insert, or resize.

The reserve() method, on the other hand does not grow the vector.

>
> On:
> Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
> Jul 27 18:10:34 EDT 2007 i686 athlon
> i386 GNU/Linux
> Using:
> g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
>
> The program below fails, but if the reserve(en)
> is uncommented, it works.


It does not. You are observing a manifestation of undefined behavior.

> Is this as expected?


There are no expectations as to how undefined behavior will show itself.

From a quality of implementation point of view, would want to see an abort
if you compile the program with assertions turned on.


> // vectst.cc 07/04/08
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> int main(int argc, const char* argv[])
> {
> int en = 10;
> vector<int> vec;
>
> // vec.reserve(en);
> for (int jj = 0; jj < en; ++jj)
> vec[jj] = jj;
> for (int jj = 0; jj < en; ++jj)
> cout << vec[jj] << endl;


Try something like

cout << vec.size() << endl;

and ponder the meaning of what you get.

>
> exit (0);
> }



Best

Kai-Uwe Bux
 
Reply With Quote
 
callumurquhart@googlemail.com
Guest
Posts: n/a
 
 
Reply With Quote
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-04-2008
On Jul 4, 7:44*am, Darío Griffo <dario.griffo.lis...@gmail.com> wrote:
> Mike -- Email Ignored wrote:
>
>
>
> > In std::vector, is reserve or resize required?

>
> > On:
> > * *Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
> > * * * Jul 27 18:10:34 EDT 2007 i686 athlon
> > * * * i386 GNU/Linux
> > Using:
> > * *g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)

>
> > The program below fails, but if the reserve(en)
> > is uncommented, it works. *Is this as expected?

>
> > // vectst.cc 07/04/08

>
> > #include <iostream>
> > #include <vector>
> > using namespace std;

>
> > int main(int argc, const char* argv[])
> > {
> > * *int * * * * * * * * *en = 10;
> > * *vector<int> * * * * *vec;

>
> > // * vec.reserve(en);
> > * *for (int jj = 0; jj < en; ++jj)
> > * * * vec[jj] = jj;
> > * *for (int jj = 0; jj < en; ++jj)
> > * * * cout << vec[jj] << endl;

>
> > * *exit (0);
> > }

>
> > Thanks,
> > Mike.

>
> No, you could do that (use reserve), or, use push_back()


reserve() does not make the vector larger. You must have meant "you
could use resize or push_back."

Ali
 
Reply With Quote
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-04-2008
On Jul 4, 7:54*am, callumurquh...@googlemail.com wrote:
> http://www.cplusplus.com/reference/stl/vector/operator[].html


If the OP did not concentrate on the vec.reserve(en), the document you
show could be used to help with the problem. But because the OP does
expect some behavior from vec.reserve(en), the problem is not with
operator[].

Ali
 
Reply With Quote
 
Mike -- Email Ignored
Guest
Posts: n/a
 
      07-04-2008
On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:

> On Jul 4, 7:54Â*am, callumurquh...@googlemail.com wrote:
>> http://www.cplusplus.com/reference/stl/vector/operator[].html

>
> If the OP did not concentrate on the vec.reserve(en), the document you
> show could be used to help with the problem. But because the OP does
> expect some behavior from vec.reserve(en), the problem is not with
> operator[].
>
> Ali


Another test using:
vec.reserve(en);
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
cout << "vec.size() = " << vec.size() << endl;

prevents the crash, but vec.size() returns zero, showing that
in this case, reserve() really does not work.

Mike.
 
Reply With Quote
 
Darío Griffo
Guest
Posts: n/a
 
      07-04-2008

acehr...@gmail.com wrote:

> reserve() does not make the vector larger. You must have meant "you
> could use resize or push_back."
>
> Ali


Yes, you're right
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      07-04-2008
Mike -- Email Ignored wrote:
> On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
>
>> On Jul 4, 7:54 am, callumurquh...@googlemail.com wrote:
>>> http://www.cplusplus.com/reference/stl/vector/operator[].html

>> If the OP did not concentrate on the vec.reserve(en), the document you
>> show could be used to help with the problem. But because the OP does
>> expect some behavior from vec.reserve(en), the problem is not with
>> operator[].
>>
>> Ali

>
> Another test using:
> vec.reserve(en);
> for (int jj = 0; jj < en; ++jj)
> vec[jj] = jj;
> cout << "vec.size() = " << vec.size() << endl;
>
> prevents the crash, but vec.size() returns zero,


What does vec.capacity() return?

> showing that
> in this case, reserve() really does not work.


I think it doesn't do what you expect. But why do you think it doesn't work?

Please consider this:
#include <iostream>
#include <vector>

int main() {
const unsigned int en = 10;
std::vector<int> v;
//
std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
v.reserve(en);
std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
//
for(unsigned int i=0; i<en; i++) {
v[i] = i;
}
//
v.push_back(-;
std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
//
for(unsigned int i=0; i<en; i++) {
std::cout << "[" << i << "] " << v[i] << std::endl;
}
}


Also, please consider this:
#include <iostream>
#include <vector>

int main() {
const unsigned int en = 10;
std::vector<int> v;
std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
v.resize(en); // there's more than one way to do it
std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
//
v[0] = -8;
v[9] = -7;
v.push_back(-9);
std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
std::copy(v.begin(), v.end(), std:stream_iterator<int>(std::cout,
" "));
}


LR
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-04-2008
Mike -- Email Ignored wrote:
> On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
>
>> On Jul 4, 7:54 am, callumurquh...@googlemail.com wrote:
>>> http://www.cplusplus.com/reference/stl/vector/operator[].html

>> If the OP did not concentrate on the vec.reserve(en), the document you
>> show could be used to help with the problem. But because the OP does
>> expect some behavior from vec.reserve(en), the problem is not with
>> operator[].
>>
>> Ali

>
> Another test using:
> vec.reserve(en);
> for (int jj = 0; jj < en; ++jj)
> vec[jj] = jj;
> cout << "vec.size() = " << vec.size() << endl;
>
> prevents the crash, but vec.size() returns zero, showing that
> in this case, reserve() really does not work.
>


No, reserve() does work. You misunderstand how it does.
vector<>::reserve changes the CAPACITY -- that is, you can use
push_back() or resize() up to the amount you've reserved without
the vector reallocating. It does not change the SIZE of the vector.

Try this:

#include <iostream>
#include <ostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v;

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << "\n";

v.reserve(200);

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << "\n";

v.resize(200);

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << endl;

return 0;
}
 
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
CASTE.BIZ one-word Indian domain eBay No Reserve Price dot HTML 0 11-28-2005 05:00 AM
reserve bandwith for certain ips jcharth@hotmail.com Cisco 4 03-06-2005 06:57 PM
XML 2004 just 3 weeks away -- view program updates -- reserve room -- register today MarionEll XML 0 10-26-2004 01:47 PM
FA: O'Reilly Java Enterprise CD Bookshelf and book (bidding starts at $1.00 with NO RESERVE) J.W. Java 0 04-19-2004 09:40 PM
How to reserve white space for a particular element in XSLT joeblack XML 3 11-04-2003 02:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57