bad_alloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • George2
    New Member
    • Dec 2007
    • 200

    bad_alloc

    Hello everyone,


    Please help to comment whether my following understanding is correct,

    1. whether or not we are using auto_ptr to allocate new object on heap (using new), there may be bad_alloc exceptions;

    2. when we met with such exceptions, we catch it (bad_alloc) and try to mininize the operation in catch handler block (since when bad_alloc occurs, it means memory is running out, we can not do anything complex in handler).

    Both are correct? Please feel free to correct me if I am wrong.


    thanks in advance,
    George
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The C++ language spec says that a bad_alloc exceprtion will be thrown when new runs out of heap memory.

    That said, this does not apply to Windows, which has a virtual memory manager. When heap memory runs lows, new just calls the virtual memory manager for another page of memory. When the memory is completely allocated, swapping occurs with the hard disc. This will continue until your hard disc fills up. In this condition, your response is very, very slow as your machine vibrates across the floor swapping memory pages with the hard disc. That would be your signal that there is a problem. For that reason, I never use a catch block with new.

    I don't know the situation with the various Unix/Linux variants.

    The bad_alloc is supposed to be thrown when there is just enough heap left to do the throw. Therefore, you should not allocate heap memory in your bad_alloc handler. If you do, you could cause the exception-inside-the-catch-block situation which is an automatic crash.

    Comment

    • George2
      New Member
      • Dec 2007
      • 200

      #3
      Thanks for your valuable advice, weaknessforcats !


      Do you think we need to call delete or delete[] to free memory if our handler for bad_alloc?

      Originally posted by weaknessforcats
      The C++ language spec says that a bad_alloc exceprtion will be thrown when new runs out of heap memory.

      That said, this does not apply to Windows, which has a virtual memory manager. When heap memory runs lows, new just calls the virtual memory manager for another page of memory. When the memory is completely allocated, swapping occurs with the hard disc. This will continue until your hard disc fills up. In this condition, your response is very, very slow as your machine vibrates across the floor swapping memory pages with the hard disc. That would be your signal that there is a problem. For that reason, I never use a catch block with new.

      I don't know the situation with the various Unix/Linux variants.

      The bad_alloc is supposed to be thrown when there is just enough heap left to do the throw. Therefore, you should not allocate heap memory in your bad_alloc handler. If you do, you could cause the exception-inside-the-catch-block situation which is an automatic crash.

      regards,
      George

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You don't delete in a bad_alloc handler because you got there due to a failure to allocate memory. A delete would probably crash your program.

        Comment

        • George2
          New Member
          • Dec 2007
          • 200

          #5
          Thanks weaknessforcats ,


          My question is answered.

          Originally posted by weaknessforcats
          You don't delete in a bad_alloc handler because you got there due to a failure to allocate memory. A delete would probably crash your program.

          regards,
          George

          Comment

          Working...