473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

vector.reserve

HI, when I try the following code, I get a segfault when compiled with
VC.NET and g++ under cygwin.

#1 vector<int> vi;
#2 vector<int>::iterator ii = vi.begin();
#3 vi.reserve(10);
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));

Here is my break down on why this code should work (fill a vector with
numbers, nevermind v[i] syntax, or v.push_back(), or the fill(...) stl
function. I really wanted to know why the use of iterators causes a problem
here).

#1 - v.size is 0;
#2 - initiate a interator and point to v.begin().
#3 - make space for 10 more elements, v.size() is still 0;
#4-6 - loop and assign each space in the vector a number. #5 is where segv
happens.

I was under the assumption that reserve will make space available to add
more element, which the iterator will be able to walk through them. I'd
appreciate any help on this issue.

Smith

Jul 19 '05 #1
5 5641
Your iterator is invalid. Obtain it after you call reserve();
Jul 19 '05 #2

Daniel Seitz wrote in message ...
Your iterator is invalid. Obtain it after you call reserve();


Oh, and it's not really "invalid", but vi.begin() == vi.end(). I hope no
confusion came of this. Read 23.1, item 7.
Jul 19 '05 #3
vector::reserve increment vector capacity,not size.
capacity and size are diff.

MSDN sample.
#include <iostream>
#include <vector>

using namespace std ;

typedef vector<int> INTVECTOR;

void main()
{
// Dynamically allocated vector begins with 0 elements.
INTVECTOR theVector;

// Add one element to the end of the vector, an int with the value 42.
theVector.push_back(42) ;

// Show statistics about vector.
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;

// Ensure there's room for at least 1000 elements.
theVector.reserve(1000);
cout << endl << "After reserving storage for 1000 elements:" << endl;
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;

// Ensure there's room for at least 2000 elements.
theVector.resize(2000);
cout << endl << "After resizing storage to 2000 elements:" << endl;
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;
}
Program Output is:
theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1

After reserving storage for 1000 elements:
theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1000

After resizing storage to 2000 elements:
theVector's size is: 2000
theVector's maximum size is: 1073741823
theVector's capacity is: 2000
HI, when I try the following code, I get a segfault when compiled with
VC.NET and g++ under cygwin.

#1 vector<int> vi;
#2 vector<int>::iterator ii = vi.begin();
#3 vi.reserve(10);
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));

Here is my break down on why this code should work (fill a vector with
numbers, nevermind v[i] syntax, or v.push_back(), or the fill(...) stl
function. I really wanted to know why the use of iterators causes a problem here).

#1 - v.size is 0;
#2 - initiate a interator and point to v.begin().
#3 - make space for 10 more elements, v.size() is still 0;
#4-6 - loop and assign each space in the vector a number. #5 is where segv happens.

I was under the assumption that reserve will make space available to add
more element, which the iterator will be able to walk through them. I'd
appreciate any help on this issue.

Smith


---
Posted via news://freenews.netfront.net
Complaints to ne**@netfront.net
Jul 19 '05 #4
On Thu, 24 Jul 2003 23:36:37 -0400, "john smith"
<as**@asdf123asdf.net> wrote:
HI, when I try the following code, I get a segfault when compiled with
VC.NET and g++ under cygwin.

#1 vector<int> vi;
This is an empty vector
#2 vector<int>::iterator ii = vi.begin();
This is a valid iterator, but since the vector is empty, being() and
end() have the same value.
#3 vi.reserve(10);
Two things : 1) These 10 new elements are not valid, _capacity_ and
_size_ are two different beasts. You increase the capacity with
reserve(), but you increase the size with, among others, push_back().

2) you just invalidated your iterator, because the location of the
elements may have changed (and they probably did).
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
Undefined behavior, since the vector is still empty. Your application
may now crash at any time (or send some porn to your girlfriend`s
email address, you never know. C++ is quite polyvalent).
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
This would have worked if you did not try to dereference your
iterator. Nothing should have been printed on the screen, since the
vector is still empty.
Here is my break down on why this code should work
It should'nt.
(fill a vector with
numbers, nevermind v[i] syntax, or v.push_back(), or the fill(...) stl
function.
what?
I really wanted to know why the use of iterators causes a problem
here).
Because you try to access an empty vector.
#1 - v.size is 0;
ok
#2 - initiate a interator and point to v.begin().
ok
#3 - make space for 10 more elements, v.size() is still 0;
that's it, you have the space for 10 elements, but it is illegal to
access these elements since they are not initialized. The only thing
you have here is raw memory. Do not try to access it.
#4-6 - loop and assign each space in the vector a number. #5 is where segv
happens. I was under the assumption that reserve will make space available to add
more element, which the iterator will be able to walk through them. I'd
appreciate any help on this issue.


std::vector::reserve() only increases the vector's capacity so that it
will avoid reallocation and it invalidates iterators. What's more,
these new elements are not valid elements, that is, you cannot access
them. The only reason for which you would want to use reserve() is to
avoid reallocation.

Jonathan

Jul 19 '05 #5

"john smith" <as**@asdf123asdf.net> wrote in message
news:bf**********@news.eecs.umich.edu...
HI, when I try the following code, I get a segfault when compiled with
VC.NET and g++ under cygwin.

#1 vector<int> vi;
#2 vector<int>::iterator ii = vi.begin();
#3 vi.reserve(10);
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));

Here is my break down on why this code should work (fill a vector with
numbers, nevermind v[i] syntax, or v.push_back(), or the fill(...) stl
function. I really wanted to know why the use of iterators causes a problem here).

#1 - v.size is 0;
#2 - initiate a interator and point to v.begin().
#3 - make space for 10 more elements, v.size() is still 0;
#4-6 - loop and assign each space in the vector a number. #5 is where segv happens.

I was under the assumption that reserve will make space available to add
more element,
Yes.
which the iterator will be able to walk through them.
No. The only way to add elements to a vector is to change its size, for
instance by calling resize, push_back or insert
I'd
appreciate any help on this issue.

Smith


reserve is just something you call for efficiency, it stops repeated
reallocations when you are adding multiple elements by using push_back or
insert. reserve also invalidates any iterators to the vector.

Here's how you should of done it.

vector<int> vi;
vi.resize(10);
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));

john
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
9
by: John | last post by:
How do I copy all the elements of a map to a vector efficiently? The vector is empty initially and needs to be populated with the map elements in sorted order. Thanks, --j
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
23
by: Mike -- Email Ignored | last post by:
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...
29
by: ab2305 | last post by:
Does standard mandates that the reserve call should initialize a container's values to its defaults, hence vector<intintVec; intVec.reserve(100) would make intVec=intVec....intVec=0 ? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.