Checking new for an error?

This is a discussion on Checking new for an error? within the C++ Programming forums, part of the General Programming Boards category; I read on the internet the other night that there isn't much you can do if a call to 'new' ...

Checking new for an error?

I read on the internet the other night that there isn't much you can do if a call to 'new' fails because there isn't enough memory left so the system will probable crash anyway.

Is this true, or is this really OS dependant? I would think that in DOS when a call no new failed it would probably crash the system because memory would be to low. On the other hand in windows OS's would it not just use the page file and not crash?

>For that reason someone invented C++.
BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!

>>Is this true
Maybe, but not likely. If new fails it could be because it couldn't find a block of consecutive bytes large enough to meet your request. If memory is way low on just about any OS these days it'll just show things down because of extra paging and use of virtual memory. A crash because of low memory is very improbable these days.

What you should do if new fails is try to recover by doing something like asking for smaller chunks, or work to gracefully terminate with what you have. If possible, you should try to recover first and then die gracefully if you can't, not vice versa. :-)

There's a few things you can do if allocation fails...
Look into std::set_new_handler(), it takes a pointer to a function which it calls when allocation fails. You can localize all your attempts to recover from a failed alloc inside your handler function.
The handler function must do one of the following:

Make more memory availible

Install a different handler

Deinstall itself

Throw an exception

Not return

On a failed alloc, your handler is called repeatedly(forever) until one of these conditions is met.

Thank you both for your quick replies!

>For that reason someone invented C++.
BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!

new allocates virtual memory not physical memory, at least, it does on a Windows platform. On an NT cored system, each process has a 4GB virtual address space, the pages required being swapped into physical memory on demand.

If you try to allocate more than 4GB I would expect new to throw a bad_alloc exception as that is the ANSI standard. I have, however, not found a compiler that does that on a PC, new tends to return NULL on an allocation failure, (which it should do if the new(nothrow) variant is used).

I would certainly not expect the OS to have any problems given sufficient pagefile space.

If you are trying to allocate more than 4GB of virtual memory, I wonder if a PC is really the right tool for the job!