CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    [RESOLVED] new operator

    Hi all,

    When operator new fails bad::alloc exception is thrown.
    Does the c++ standard guarantee that the pointer will be set to NULL ?

    Many thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: new operator

    No. Anyway, because of the exception, the pointer would be left unchanged.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: new operator

    AFAIK, when exception was thrown, stack unwinding is happen and all the local copy get destroyed. Why you said the pointer was unchanged ?

    Does it related to new allocate memory on heap ?

    Thanks.
    Thanks for your help.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: new operator

    Quote Originally Posted by Peter_APIIT
    AFAIK, when exception was thrown, stack unwinding is happen and all the local copy get destroyed. Why you said the pointer was unchanged ?
    If the exception causes control to reach a point where the pointer goes out of scope, then yes, the pointer would be destroyed. However, it is not necessarily the case that the pointer will go out of scope since it could be assigned to in a try block where the exception is caught in the catch block.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: new operator

    Sorry for my stupidity.

    Do you have any example code that explain your idea ?

    Thanks.
    Thanks for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: new operator

    Quote Originally Posted by Peter_APIIT
    Do you have any example code that explain your idea ?
    Well, a simple but rather silly example for some type T:
    Code:
    T* p = 0;
    try
    {
        p = new T;
    }
    catch (const std::exception&)
    {
        // do nothing
    }
    delete p;
    My main point is that because the exception is thrown, the assignment does not take place, so the pointer retains its value. If the pointer happens to be destroyed immediately after that, so be it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Dec 2005
    Posts
    445

    Re: new operator

    Thanks you!


    EDIT: How do I mark the message as a resolved?? Thank you

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: new operator

    You're welcome
    You can use the thread tools to mark the thread as resolved.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: [RESOLVED] new operator

    Just a complement...

    Still, it's possible to use new in the "old" form. This is done with the nothrow version of new and is guaranteed by the Standard to return 0 in case of failure.

    Code:
    SomeType p = new (std::nothrow) SomeType;
    if (!p)
    {
      //Failure...
    }

  10. #10
    Join Date
    Dec 2005
    Posts
    445

    Re: [RESOLVED] new operator

    Quote Originally Posted by ltcmelo View Post
    Just a complement...

    Still, it's possible to use new in the "old" form. This is done with the nothrow version of new and is guaranteed by the Standard to return 0 in case of failure.

    Code:
    SomeType p = new (std::nothrow) SomeType;
    if (!p)
    {
      //Failure...
    }
    Watch out from std::nothrow() when there is a chance that your code may be used in environments that define thier own version of new operayor (MFC for example).
    http://www.codeguru.com/forum/showthread.php?t=470168

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