bad_alloc

This is a discussion on bad_alloc within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Please help to comment whether my following understanding is correct, 1. whether or not we are using auto_ptr ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    1. auto_ptr is used to ensure that the memory is freed. It has nothing at all to do with allocation. So yes, whether or not you use it, the new operator may throw. Unless you use the non-throwing variant.

    2. Catching bad_alloc is something that is typically only done in two places:
    1) At the top level of your program, to ensure graceful termination in OOM situations (there's not much you can do about them other than terminating).
    2) Around areas that allocate an unusually large amount of memory. In such cases, you can just abort the memory-intensive operation and keep your program alive.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    1. auto_ptr is used to ensure that the memory is freed. It has nothing at all to do with allocation. So yes, whether or not you use it, the new operator may throw. Unless you use the non-throwing variant.

    2. Catching bad_alloc is something that is typically only done in two places:
    1) At the top level of your program, to ensure graceful termination in OOM situations (there's not much you can do about them other than terminating).
    2) Around areas that allocate an unusually large amount of memory. In such cases, you can just abort the memory-intensive operation and keep your program alive.
    If we met with bad_alloc, do we need to use delete or delete[] to free the memory in exception handler block?


    regards,
    George

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by George2 View Post
    If we met with bad_alloc, do we need to use delete or delete[] to free the memory in exception handler block?
    No. bad_alloc means there wasn't enough memory to allocate what you asked for. So since it didn't allocate anything, you don't need to delete anything.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your clarification, cpjust!


    My question is answered.

    Quote Originally Posted by cpjust View Post
    No. bad_alloc means there wasn't enough memory to allocate what you asked for. So since it didn't allocate anything, you don't need to delete anything.

    regards,
    George

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21