new throws "bad"

This is a discussion on new throws "bad" within the C++ Programming forums, part of the General Programming Boards category; When you try to allocate memory with new and you don't have enough space it throws exception with "bad memory ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    new throws "bad"

    When you try to allocate memory with new and you don't have enough space it throws exception with "bad memory allocation" in what field of it. I looked at the source code and saw that it exactly throws that exception when there's not enough space. I imagined why what field is not "Not enough memory". That way we could know why allocation failed.

    I am using VS2008 SP1.

    Any idea?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by siavoshkc View Post
    When you try to allocate memory with new and you don't have enough space it throws exception with "bad memory allocation" in what field of it. I looked at the source code and saw that it exactly throws that exception when there's not enough space. I imagined why what field is not "Not enough memory". That way we could know why allocation failed.

    I am using VS2008 SP1.

    Any idea?
    Using the debugger, configure it to break when an exception is thrown. The call stack will show what's going on.

    How to: Break When an Exception is Thrown
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think he's asking why MS didn't choose a better name in their error handling, not how to trap an error.

    As to why they chose the error message, I have no idea. If there's really only one possible message, then presumably they wanted to be very general about what it was.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by tabstop View Post
    I think he's asking why MS didn't choose a better name in their error handling, not how to trap an error.

    As to why they chose the error message, I have no idea. If there's really only one possible message, then presumably they wanted to be very general about what it was.
    The actual C++ exception thrown is called std::bad_alloc. So a "bad allocation" is exactly what happens.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by brewbuck View Post
    The actual C++ exception thrown is called std::bad_alloc. So a "bad allocation" is exactly what happens.
    But the implementation is allowed to put anything it wants in the .what member of that exception.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Quote Originally Posted by tabstop View Post
    I think he's asking why MS didn't choose a better name in their error handling, not how to trap an error.
    Yes, this is what I meant.

    Quote Originally Posted by tabstop View Post
    As to why they chose the error message, I have no idea. If there's really only one possible message, then presumably they wanted to be very general about what it was.
    If there is only one message, why there should be any message?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by siavoshkc View Post
    Yes, this is what I meant.


    If there is only one message, why there should be any message?
    Because std::bad_alloc derives from std::exception, and therefore must implement what(). How could the message be improved?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by brewbuck View Post
    Because std::bad_alloc derives from std::exception, and therefore must implement what(). How could the message be improved?
    Quote Originally Posted by siavoshkc View Post
    "Not enough memory".
    I am forced to type something here as I cannot make a post of only quotes.

    (And as I said above, if there's only the one message, then there may well be other points of failure that they are worried about that you missed/don't exist, so they went with a generic message.)

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by tabstop View Post
    (And as I said above, if there's only the one message, then there may well be other points of failure that they are worried about that you missed/don't exist, so they went with a generic message.)
    Kind of what I'm getting at. Even if new fails because memory couldn't be found, the message "Out of memory" may not really explain what happened adequately. It could be that some other, usually dormant, program on the system woke up momentarily, used a huge chunk of memory, then released it and went back to sleep -- "out of memory" in that case is really just an unfortunate sequence of events -- if your code had requested memory a fraction of a second later, it might have succeeded. There are so many reasons why a particular memory allocation could fail that really, all you can say is that it failed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Quote Originally Posted by MSDN
    The value returned by what is an implementation-defined C string.
    So it is not defined by standard and when std library implementor writes code, he/she exactly knows where and when exceptions are thrown. Thus a very clear text message on exception can be defined.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    If this is such a big deal for you, do the work.

    You only need to: create a class derived from `std::bad_alloc'; replace the current allocation handler; decide on a limiting factor for retries; provide an API to set and get the current limiting factor; determine a way distinguish between application errors, library errors, system error, user errors, temporarily unavailable resources, permanently unavailable resources, and attempts to obtain the resource reaching the currently set limiting factor; provide a context sensitive string representing all the relevant conditions.

    Of course, this is just off the top of my head. I'm sure others could probably add to the requirements.

    Enjoy.

    Soma

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see why it matters what the error message is?
    If you catch a std::bad_alloc, your new failed, end of story. You can either, give up, try again or ask for a smaller amount of memory.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler throws error due to the use of "SEEK_SET"
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-26-2008, 02:53 PM
  2. Vector class throws bad_alloc
    By animeaholic in forum C++ Programming
    Replies: 7
    Last Post: 04-28-2008, 01:48 AM
  3. program throws assert after deleting a resource
    By hanhao in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2007, 09:57 PM
  4. parition magic "BAD" partition
    By valis in forum Tech Board
    Replies: 5
    Last Post: 05-23-2006, 01:05 PM
  5. File Opening Throws a Illegal Op.
    By Crossbow in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2004, 03:17 PM

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