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

Sizing a STL vector object to available memory

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
1 1316
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
7
by: hpy_awad | last post by:
I need to make the size of an array as variable read from screen . How can I do it ?
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
6
by: Siam | last post by:
Hi, I'm a little new to stl so bear with me...Say I have the following code: vector<intvec; int i = 3; vec.push_back(i); i=4; cout<<vec.at(0)<<endl;
5
by: mygulamali | last post by:
Hi all! So I'm trying to determine how much memory is being used by the following class member in my code: vector<map<unsigned int, double Would the snippet below be the right way of going...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
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
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.