CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: operator new

  1. #1
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    operator new

    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


  2. #2
    Join Date
    Apr 1999
    Posts
    37

    use _set_new_handler

    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 );



  3. #3
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: use _set_new_handler

    Hi Raj,

    Are you referring to VC6 ?

    Brendan


  4. #4
    Join Date
    Apr 1999
    Posts
    37

    Re: use _set_new_handler

    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);


  5. #5
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: use _set_new_handler

    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




  6. #6
    Join Date
    Apr 1999
    Posts
    37

    Re: use _set_new_handler

    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.




  7. #7
    Guest

    Re: operator new

    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


  8. #8
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: operator new

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured