[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Re: vector container resize std::bad_alloc

Per

12/5/2008 12:18:00 PM

On 2008-12-05, joecook@gmail.com <joecook@gmail.com> wrote:
> On Dec 5, 8:01 am, fabian....@gmail.com wrote:
>> std::allocator will throw a std::bad_alloc exception, when it does the
>> array will be about size 40,000. Are they are ways to increase the
>> amount of memory my program can use?
>
> This is OS dependant, and unrelated to c++. Unfortunately, the
> answer is probably 'no'.
>
> Because of the way resize() works, you may be trying to allocate more
> memory than you really need. If you know in advance what the maximum
> is, you can try using resize() to pre-allocate the memory.

Perhaps a std::list will suffice in that case since no extra memory
space will be allocated beforehand. Or perhaps a std::deque will not
allocatede the same amount of memory.

/Per
1 Answer

Juha Nieminen

12/6/2008 2:33:00 PM

0

Per wrote:
> Perhaps a std::list will suffice in that case since no extra memory
> space will be allocated beforehand. Or perhaps a std::deque will not
> allocatede the same amount of memory.

std::deque consumes much less memory than std::list, so it's the
preferred method. (Basically the only situation where you want to use
std::list is when you want to be able to add or remove elements from the
middle in O(1) (assuming you already have an iterator to the position to
be added or removed) or for O(1) list splicing.)