By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Sizing a STL vector object to available memory

P: 1
Hello,

I am trying make a vector of int that are sized to the max size (minus ~ 10-20% for other stuff to run) of memory. I have no clue how to do this. I thought max_size, reserve would do the trick but not! I then tried trial and error but the vector class throws a terminate exception and I don't have the opportunity to "back off" of my request so that my vector object fits. I wanted to avoid using new and delete since I am really using "widgets" instead of int but the same concept applies to ints as well. Anyone have a clue?
Jun 9 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You use reserve to allocate a vector of a given size. You specify the number of objects. Thre reserve methods calculates the amount of memory needed.

Be wary using vector with a class of your own. STL assumes you know what you are doing. Be sure your classes have copy constructors, destructors, and assignment operators. Ohterwise, as the objects are moved inside the constained you may see your database disintegrate.

A vector of pointers is tricky. The pointers may get deleted and take youe object with it. I recommend a vector of handles in this case. There is an article on Handle classes in the C/C++ Articles forum.

Lastly, a code sample would help.
Jun 9 '07 #2

Post your reply

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