Re: when can new fail to accocate memory??
On May 28, 12:03 pm, "doublemaster...@gmail.com"
<doublemaster...@gmail.com> wrote:
Is that only when system has insufficient memory??
Technically, it's anytime the runtime library decides that it
can't or doesn't want to give you memory. Typically, the
runtime library will try to get more memory from the system, and
operator new will only fail if this fails, but there's no
requirement either way. On one hand, the runtime might attempt
to create more virtual memory, on systems where that's possible,
or simply wait awhile, and try again, on the hope that some
other process will have freed memory in the meantime. On the
other, it might decide to artificially restraint the process to
some maximum, for who knows what reason. (My debugging operator
new will "fail" on command, for test purposes.)
In practice, such cases are rare---most operator new simply call
malloc, and most malloc request more memory from the system, if
they need it, and only fail if that request fails.
is there any ways to know why new has failed?
Not really. For the most part, you can simply suppose that it
is because the system ran out of memory to allocate.
(i am on using unix)
You might, but I doubt it. Unix reports the error reason in
errno, but the only documented error for malloc in Posix is
ENOMEM: insufficient storage available.
Note that some Unix, at least by default, don't fail when there
is insufficient memory. (Linux is in this category.) They just
return a pointer which will core dump when you use it.
--
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