473,387 Members | 3,750 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,

Is it not legal to access an element that has been reserved?

For example:

vector<int> x;
x.reserve(10);

x[0] = 1;

This is throwing an exception with VS 2005, but worked fine for VS 2002.

From what I understand, reserve allocated memory but does not adjust the
internal "size" member. So technically, I should be able to access the
elements?
Apr 29 '06 #1
5 2065
quat wrote:
Hi,

Is it not legal to access an element that has been reserved?
Yes, it is not.
For example:

vector<int> x;
x.reserve(10);

x[0] = 1;

This is throwing an exception with VS 2005, but worked fine for VS 2002.

From what I understand, reserve allocated memory but does not adjust the
internal "size" member.
Reserve allocates memory, but doesn't create any objects. The above vector
contains 0 objects of type int, but you can add (e.g. with push_back) at
least 10 without a reallocation happening. You can't access any objects
yet, because there are still none in the vector. Whatever "internal"
members a vector might or might not have is of no relevance for that.
That's an implementation detail you aren't supposed to care about.
So technically, I should be able to access the elements?


No, because there are no elements yet. If you want a vector with 10
elemenents, try using resize() instead of reserve().

Apr 29 '06 #2
No, because there are no elements yet. If you want a vector with 10
elemenents, try using resize() instead of reserve().


I can accept this. However, I don't get how an allocation can occur without
creating objects. Presumably, reserve does something like this:

data = new type[n]; // where n = amount to reserve.

Thus data[j] should be accessible for 0 <= j < n.
Apr 29 '06 #3
quat wrote:
No, because there are no elements yet. If you want a vector with 10
elemenents, try using resize() instead of reserve().

I can accept this. However, I don't get how an allocation can occur
without
creating objects. Presumably, reserve does something like this:

data = new type[n]; // where n = amount to reserve.


There are ways of allocating raw memory without creating objects (using
global operator new), and there are ways of turning that raw memory (using
placement new).
Thus data[j] should be accessible for 0 <= j < n.


It might be, but there is no guarantee, unless you actually resize the
vector.

Apr 29 '06 #4
There are ways of allocating raw memory without creating objects (using
global operator new), and there are ways of turning that raw memory (using
placement new).


Okay I didn't know this. Thanks.
Apr 29 '06 #5
quat wrote:
There are ways of allocating raw memory without creating objects (using
global operator new), and there are ways of turning that raw memory (using
placement new).


Okay I didn't know this. Thanks.

If you want to create elements in a vector en-masse, use
resize() not reserve().
Apr 30 '06 #6

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

Similar topics

5
by: john smith | last post by:
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 ...
2
by: Alex Vinokur | last post by:
Method vector.reserve() returns no value. How can we prevent the following situation? ====== foo.cpp ====== #include <climits> #include <vector> #include <iostream> using namespace std; ...
2
by: Gary Kuehn | last post by:
Is Reserve guaranteed to allocate contiguous memory? How safe is the following: vector<char> vbuff; int sz = numeric_limits<short int>::max();
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...
3
by: pkirk25 | last post by:
vector<stringbuf_string; buf_string.reserve(256); vector<intbuf_mat_prices; buf_mat_prices.reserve(1000); During loops I fill the vectors and then I empty them with commands like...
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...
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...
3
by: jason.cipriani | last post by:
What is the official word on what happens if I call reserve() on an std::vector, and specify a capacity that is less than the current *size* (not capacity) of that vector? Is it supposed to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.