Click to See Complete Forum and Search --> : operator new


Brendan Cullen
May 26th, 1999, 02:22 AM
Hi,

I'm using Visual C++ 5.0. And help says. " If there is insufficient memory for the allocation request, by default operator new returns NULL." You can change this default behaviour but the help is unsatisfactory. In VC 6, has the default behaviour changed to automatically throw an exception instead of returning null ?

Regards,

Brendan

RajM
May 26th, 1999, 07:30 PM
you can use the C runtime routine to set your own new exception handler.
int handle_program_memory_depletion( size_t )
{
Your code to handle exceptions
}
PNH old_handler = _set_new_handler( handle_program_memory_depletion);
_set_new_handler( old_handler );

Brendan Cullen
May 27th, 1999, 02:19 AM
Hi Raj,

Are you referring to VC6 ?

Brendan

RajM
May 27th, 1999, 10:50 AM
Its a part of the C runtime libaray(libc.lib) and it has been around for while.Its not something new which started with vc++ 6.0.
In fact,u can use the same handler for handling failures of malloc by calling this function at startup
_set_new_mode(1);

Brendan Cullen
May 27th, 1999, 11:06 AM
Hi Raj,

What I'm trying to determine is this : With VC++6.0, can "new" return a NULL ? The standard says it shouldn't.

Brendan

RajM
May 27th, 1999, 12:26 PM
Hi Brendan,
In VC 6.0 ,the behavior of new depends on where u are using it.If u are using it under MFC,if new fails it doesnt return at all.It throws a CMemoryException.I tried this code on VC 6.0 MFC Program .The value of pData was never changed.

PBYTE pData=(PBYTE)100;
TRY{
pData=new BYTE[1000*1024*1024];
}
CATCH(CMemoryException,e)
{
e->ReportError();
}
END_CATCH
For a Console app under VC 6.0,the behaviour is different.
It returns a NULL.This is what i observed when i ran the code below.
main()
{
PBYTE pData=(PBYTE)100;
pData=new BYTE[1000*1024*1024];
if(pData == NULL)
{
it has failed.
}
}

My guess is that if u are using MFC,the framework installs it own new exception handler and throws the CMemoryException.
So the summary is in VC 6.0 new returns NULL if it fails and there is no new handler set and no MFC support.

May 27th, 1999, 01:21 PM
In 5.0 an exception was also thrown by the new operator instead of new returning null. The problem is if you try to override mfcs implementation of new you will also have to handle the mfc source base because when mfc source uses new the next statement never checks to see if the pointer is good because the excetion would have been thrown so it just uses the pointer if you over-ride you will have MFC source crash when new doesn't throw an exception and roll back the local stack

Brendan Cullen
May 28th, 1999, 02:12 AM
Hi,

>>In 5.0 an exception was also thrown by the new operator instead of new returning null. The problem is if you try to override mfcs implementation of new you will also
have to handle the mfc source base because when mfc source uses new the next statement never checks to see if the pointer is good because the excetion would have
been thrown so it just uses the pointer if you over-ride you will have MFC source crash when new doesn't throw an exception and roll back the local stack

Point taken - but why does VC++ 5.0 help say that new returns null by default if there's a failure to allocate ? Thehelp says : "If there is insufficient memory for the allocation request, by default operator new returns NULL. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument. Alternately, you can choose to have new throw a C++ exception (of type xalloc) in the event of a memory allocation failure."

Brendan