Re: Memory issue
* James Kanze:
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.
No, that theory isn't practice. I think what you would have meant to write, if
you thought about it, would have been "which means that, provided there are no
other processes using much memory, an ideal OS won't page". And some systems may
work like that, and when you're lucky you don't have any other processes
competing for actual memory. :-)
Andy Chapman's comment else-thread was more relevant, I think.
Because with enough RAM you can conceivably hit the limit of the address space
and get a std::bad_alloc before the OS starts trashing.
(4GB for both seems to be a common figure,
even on 64 bit systems.) The phenomenon you describe would
mainly apply to older systems.
Well, my experience and interest in recent years has mostly been with Windows.
AFAIK it's quite difficult to make Windows refrain from paging.
This is a problem for some people implementing Windows based servers.
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.
Example?
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.
That's what I wrote, yes.
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.
Yeah, I lost the context of std::vector, sorry.
* 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.
Yes, that's correct.
Cheers,
- Alf