In a school project of mine, I was requested to create a program without using STL
In the program, I use a lot of
Pointer* = new Something;
if (Pointer == NULL) throw AllocationError();
My questions are about allocation error:
- is there an automatic exception thrown by
newwhen allocation fails? - if so, how can I catch it if I’m not using STL (
#include "exception.h")? - is
NULLtesting enough?
Thank You.
I’m using eclipseCDT(C++) with MinGW on Windows 7.
Yes, the new operator will automatically thrown an exception if it cannot allocate the memory.
Unless your compiler disables it somehow, the new operator will never return a NULL pointer.
It throws a
bad_allocexception.Also there is a
nothrowversion of new that you can use:This version returns a null pointer if the memory cannot be allocated. But also note that this does not guarantee a 100%
nothrow, because the constructor of the object can still throw exceptions.Bit more of information: http://msdn.microsoft.com/en-us/library/stxdwfae(VS.71).aspx