CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    19

    new and memory allocation error

    Another quick question!

    When standard new cannot allocate the requested storage in C++ which of the following does it do? (standard behaviour). (can be more than one answer)

    1 - throws a bad_alloc exception
    2 - throws an insufficient_memory exception
    3 - returns null
    4 - returns silently with no effect on the expression
    5 - logs an error message to the mem_err.log file


    I would have said definitly 1, any other thoughts or interesting info about the others?

    Is there any way I can force this error so I can try it out?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: new and memory allocation error

    If the nothrow version of new is used, then 3 would be correct. However, the question probably isn't asking about that.

  3. #3
    Join Date
    Sep 2009
    Posts
    19

    Re: new and memory allocation error

    thanks for the reply, so would you think it is just 1?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: new and memory allocation error

    Well, there would be no point in throwing two exceptions for the same thing, and if an exception is thrown then it doesn't make sense to talk about a return value. Plus I can't imagine standard C++ doing any logging you didn't ask it to do.

    I suppose #4 might make sense depending on what they mean by "returns silently". Jumping directly to the catch block (if any) will certainly not have any effect on the expression...

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