473,325 Members | 2,792 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,325 software developers and data experts.

reserve() in vector?

hi,
I like to preallocate a vector of an element X, my question is after
calling reserve(), is there a guarantee that memory is contiguous?

Oct 3 '06 #1
3 2525
we*****@yahoo.com wrote:
I like to preallocate a vector of an element X, my question is after
calling reserve(), is there a guarantee that memory is contiguous?
Yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #2
Victor Bazarov wrote:
we*****@yahoo.com wrote:
>I like to preallocate a vector of an element X, my question is after
calling reserve(), is there a guarantee that memory is contiguous?

Yes.
Specifically 23.2.4/1 (ISO/IEC 14882:2003).

"The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocatorwhere T is some type other than bool, then it
obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."
Oct 3 '06 #3
we*****@yahoo.com wrote:
hi,
I like to preallocate a vector of an element X, my question is after
calling reserve(), is there a guarantee that memory is contiguous?
As others have said, the memory is guaranteed to be contiguous.
However, from the wording of your question it sounds like you may
misinterpret the intent of the reserve function. Are you trying to do
something like this?

void some_library_function(char * p, size_t len)
{
// Write to p.
}

int main()
{
std::vector<charv ;
v.reserve(100) ;
some_library_function(&v[0], 100) ; // ERROR!!
}

Calling reserve will allocate enough memory to hold the specified
number of objects, but it doesn't give you access to that memory. That
is, after a call to reserve, the vector still contains the same number
of elements as before to call to reserve. If your intent is to do
something like the above, use resize instead of reserve.

--
Alan Johnson

Oct 4 '06 #4

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 ...
3
by: Tino | last post by:
In using std::priority_queue, I'm concerned about the expense of memory allocation and copying as the priority_queue grows large. std::vector has reserve() to address this concern, though there...
3
by: Liran Shaul | last post by:
I was wondering: how come the stl map class does not have a reserve function like the useful vector::reserve function? I was thinking about implementing my own reserve class, by providing my...
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...
5
by: quat | last post by:
Hi, Is it not legal to access an element that has been reserved? For example: vector<int> x; x.reserve(10); x = 1;
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...
3
by: JDT | last post by:
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.