This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Can new operator return NULL ?


> I am looking how to force that new operator will return NULL instead of
> std::bad_alloc exception when lack of memory.
> I have on the beginning of code  mlockall(MCL_FUTURE); this forces malloc()
> to return NULL if out of nemory,
> but new operator throws exception.
> I know that in Windows Visual C++ throwing new is set by chosing standard
> C++ library and nonthrowing new is from main CRT,
> so in VS I can choose one of these two features.
> Is possible in C++ code compiled with g++ to force new operator to return
> NULL instead of std::bad_alloc ?

You need the functionality of "nothrow new" in the C++ standard.
Syntax is something like the following...

#include <new>

ptr2object = new(std::nothrow) ClassType( ... ) ;

I'm not sure of what g++ provides, and my installation is hosed at the moment 
(so I can't test this), but gcc is pretty compliant and up-to-date.
I'd be surprised if it didn't have nothrow new...

However, I do not know if there is a way to get similar functionality without
modifying code (calls to new).

Hope this helps,
   Michael


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]