473,385 Members | 1,912 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,385 software developers and data experts.

Does vector function reserve() allocates space?

bajajv
152 100+
Hi, I have gone thru a tutorial about vectors in cppreference.com and it says that the reserve() function in vector allocates space for vector. But if I allocate some space for vector and try to give some value using the [] operator, it crashes.

Expand|Select|Wrap|Line Numbers
  1. vector<int> vect;
  2. vect.reserve(5);
  3.  
  4. cout << "Capacity - " << vect.capacity() << endl; //it gives 5
  5.  
  6. vect[0] = 10; //it crashes, saying out of range value given
  7.  
  8.  
What is the use of vector function() reserve?
Jan 23 '11 #1

✓ answered by horace1

reserve() requests a change in vector capacity ready for a change in size later
http://www.cplusplus.com/reference/stl/vector/reserve/

try running
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. vector<int> vect;
  4. vect.reserve(5);
  5.  
  6. cout << "Capacity - " << vect.capacity() << endl; //it gives 5
  7. cout << "size - " << vect.size() << endl; //it gives 5
  8. vect.resize(5);
  9. cout << "Capacity - " << vect.capacity() << endl; //it gives 5
  10. cout << "size - " << vect.size() << endl; //it gives 5
  11. vect[4] = 10; //it crashes, saying out of range value given
  12. vect.at(4) = 10; //it crashes, saying out of range value given
  13. cout << vect[4] << endl;
  14.  
  15. }
one would use reserve() to allocate capacity so that the vector does not have to be reallocated every time the current capacity is exceeded
I would also recommend that you use the at() method rather than operator[] as it checks for out of range and throws an exception - operator[] does not throw an exception - the program may carry on and crash later or give a memory segmentation error

3 2299
horace1
1,510 Expert 1GB
reserve() requests a change in vector capacity ready for a change in size later
http://www.cplusplus.com/reference/stl/vector/reserve/

try running
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. vector<int> vect;
  4. vect.reserve(5);
  5.  
  6. cout << "Capacity - " << vect.capacity() << endl; //it gives 5
  7. cout << "size - " << vect.size() << endl; //it gives 5
  8. vect.resize(5);
  9. cout << "Capacity - " << vect.capacity() << endl; //it gives 5
  10. cout << "size - " << vect.size() << endl; //it gives 5
  11. vect[4] = 10; //it crashes, saying out of range value given
  12. vect.at(4) = 10; //it crashes, saying out of range value given
  13. cout << vect[4] << endl;
  14.  
  15. }
one would use reserve() to allocate capacity so that the vector does not have to be reallocated every time the current capacity is exceeded
I would also recommend that you use the at() method rather than operator[] as it checks for out of range and throws an exception - operator[] does not throw an exception - the program may carry on and crash later or give a memory segmentation error
Jan 23 '11 #2
weaknessforcats
9,208 Expert Mod 8TB
When you reserve space in a vector does not add elements. The operator[] ony works for elements that exist and can cuase a crash if the index value is greater than the size of he vector.

This behavior duplicates the behavior of the [] operator used on an array. vector is required to implement an array hence the same behavior in accessing loctions outside the array bounds.
Jan 23 '11 #3
bajajv
152 100+
Thanks for the responses.
Jan 29 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: joeblack | last post by:
Elements like <textarea> must be in form of <textarea ...></textarea> instead of <textarea .../>. How to do it or reserve a white space for it in XSLT tranformation. Thanks a lot
35
by: Ram Laxman | last post by:
I have used vector in the VC++ compiler. I have included #include <string> #include <algorithm> #include <vector> std::vector<int> field; std::vector <int>::size_type i;
2
by: Matthias Kaeppler | last post by:
Hi, I'm holding a vector of boost::filesystem::pathS and a vector of pointers to them. I use the reserve member function on both to reserve space for 500 files. That means, for the 501th file...
8
by: Alex Vinokur | last post by:
What is relation between std::vector's reserve() and erase()/clear()? vector<int> v; v.reserve(100); v.resize(100); v.erase(v.end()); How many elements are reserved here: 100 or 99?
3
by: ad | last post by:
I download the source of Portal Starter Kit form http://www.asp.net/Default.aspx?tabindex=8&tabid=47 I find there are Register tag in many .aspx, like this HtmlModule.ascx , the first line is ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
2
by: Samant.Trupti | last post by:
Hi, Does main function support unicode? int main( int argc, char** argv ) can I say int mainw( int argc, wchar_t** argv )? Thanks Trupti
1
by: zeeshan708 | last post by:
lets suppose that we have a C++ standard string object S. and we have const char * S2. when we do S2=S.data(); then what happens? does this function dynamically allocates space to S2 and writes...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.