Oct 5, 2010 at 10:23am UTC
how will u know if memory is available before using new operator? any mechanism
Oct 5, 2010 at 10:43am UTC
You can't but you can catch an exception in case new
fails
Oct 5, 2010 at 12:26pm UTC
You should not rely on the exception, either. Many systems won't throw it.
I can allocate easily 2 Gigs of memory on a system having just 256 MB of physical memory installed, and no exception is thrown on Linux. The app will get killed, when it tries to use all those memory.
Last edited on Oct 5, 2010 at 12:27pm UTC
Oct 5, 2010 at 1:38pm UTC
xorebxebx: set your overcommit_memory flag to 0 to fix that.
But yes, in general, most apps handle out-of-memory conditions by unexpectedly crashing, as there is
little else that can be done.
Oct 5, 2010 at 2:23pm UTC
Include the <new> library and add the (nothrow) parameter. Then check if the outputted pointer is NULL. If it is, the new-call failed.
Oct 5, 2010 at 5:34pm UTC
Either way, include it if you want the (nothrow) parameter to be usable.
Oct 5, 2010 at 9:14pm UTC
No, because if new fails on line 2, it will throw std::bad_alloc; it will not return NULL.
Go off now and fix all your code.
Oct 8, 2010 at 5:01am UTC
Thanks. That's what I get for using a 12 yo textbook. It used to work that way!