473,783 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2547
we*****@yahoo.c om 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.c om 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.c om 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_fu nction(char * p, size_t len)
{
// Write to p.
}

int main()
{
std::vector<cha rv ;
v.reserve(100) ;
some_library_fu nction(&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
5665
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 for(int i = 0; i < 10; ++i) { #5 *ii++ = 4; #6 } #7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
3
4476
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 doesn't seem to be anything like this for priority_queue. Also, I don't know anything about how priority_queue is implemented (specifically, MSVC++ 6.0), so maybe I don't have anything to worry about. Guidance, please? Regards, Ryan
3
17211
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 own allocator, inherting a new class from map, and add it a reserve function that will work on my allocator and ask it to reserve memory for future use. however, I don't know how to call the map's Allocator instance in order to reserve the memory....
7
3014
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 501st element is going to cause some more allocation -- for arguments sake if the vector re-grows to accomodate 1000 elements, I play around with it and completely erase everything in it after I am done. Now how much does the vector hold? Do I...
5
2083
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
2605
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 vectors using push_back. I know that I will be reading in 5000 elements into each vector, so I use reserve: ifstream f( "file.txt" ); if(f.is_open()) {
3
2050
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 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<intv; v.reserve(100);
23
3482
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 4.1.2-12) The program below fails, but if the reserve(en)
29
2210
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.