Re: Memory issue
On Aug 10, 9:09 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* kathy:
I want to use std::vector::pushback function to keep data in
RAM. What will happened if no more memory available?
In practice, what happens on a modern system as free memory
starts to become exhausted and/or very fragmented, is that the
system slows to a crawl, so, you're unlikely to actually reach
that limit for a set of small allocations.
On modern systems, it's not rare for the actual memory to be the
same size as the virtual memory, which means that in practice,
you'll never page. (4GB for both seems to be a common figure,
even on 64 bit systems.) The phenomenon you describe would
mainly apply to older systems.
However, a very large allocation might fail.
Or not. Some OS's don't tell you when there's not enough
virtual memory, in which case, the allocation works, but you get
a core dump when you use the memory.
In that case, the C++ standard guarantees a std::bad_alloc
exception as the default response, provided the allocation at
the bottom was via 'new'.
Which is required in the default allocator.
This default response can be overridden in three ways:
* By replacing the class' operator new (this is just an
unfortunate name for the allocation function; it's
*called* by a 'new' expression, which after that call
proceeds to call the specified constructor, i.e. you're
not overriding what a 'new' expression does by defining
operator new).
Interestingly enough, this has absolutely no effect on
std::vector---std::vector< T > will always use ::operator new,
even if T has a class specific allocator.
* By replacing the global namespace operator new (ditto
comment).
* By installing a new-handler (see set_new_handler, I think
the name was).
* By instantiating the vector with a custom allocator.
In all cases, however... The allocator must return a valid
pointer, if it returns. So the only possible ways of handling
an error are to throw an exception or to terminate the program.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34