Thread: bad_alloc

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

    bad_alloc

    Hello everyone,


    I am wondering except when there is no memory on heap, are there any other situations when we will get bad_alloc exceptions? For example, invalid input of the size (e.g. very huge number or zero or negative number) will cause exception?


    thanks in advance,
    George

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I think there is ample documentation on the web to tell you the answer to that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I only find out of memory as one situation, but I am not sure about others. MSDN also does not provide formal description when there will be bad_alloc exception.

    Quote Originally Posted by Salem View Post
    I think there is ample documentation on the web to tell you the answer to that.

    regards,
    George

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > MSDN also does not provide formal description when there will be bad_alloc exception.
    You should really look up what the standard says, not what a specific vendor says.

    Final draft of C++98

    Or maybe a pukka electronic copy, http://www.ncits.org/cplusplus.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Zero is a valid input to the allocator function. Huge numbers are also valid, although it's likely that there's not enough memory available. Negative numbers are technically impossible, because the argument is an unsigned number.

    operator new's behaviour is defined as either returning valid memory or throwing a bad_alloc. There's no other possibility. It may not return null. It may not throw anything else. Therefore, any allocation failure of any kind must be reported as a bad_alloc or lead to immediate program abortion. (A corruption of the heap management data, for example, would probably lead to termination.)
    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

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    operator new's behaviour is defined as either returning valid memory or throwing a bad_alloc. There's no other possibility. It may not return null.
    What? Since when?
    Although I've never used it, I've known about the nothrow version of new for a long time: http://www.informit.com/guides/conte...eqNum=170&rl=1

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    operator new's behaviour is defined as either returning valid memory or throwing a bad_alloc. There's no other possibility. It may not return null. It may not throw anything else.
    But then is the output of my compiler wrong if the following program prints "Other error"?
    Code:
    #include <iostream>
    #include <stdexcept>
    
    class A
    {
        public:
        A() { throw 1; }
    };
    
    int main()
    {
        try {
            A* a = new A;
        }
        catch (std::bad_alloc&) {
            std::cout << "Allocation error\n";
        }
        catch (int) {
            std::cout << "Other error\n";
        }
    }
    On one hand it is good to know that the problem was not allocating the memory but an exception was thrown from the constructor of the object. On the other hand it would mean that simply catching bad_alloc is not sufficient in all cases.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    anon: The exception was thrown by the constructor and came from the new operator, not operator new.

    cpjust: nothrow new is another story.
    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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    But then is the output of my compiler wrong if the following program prints "Other error"?
    I don't think so, since it is the constructor that threw the exception.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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


    Is nothrow new supported in Visual Studio 2008?

    Quote Originally Posted by cpjust View Post
    What? Since when?
    Although I've never used it, I've known about the nothrow version of new for a long time: http://www.informit.com/guides/conte...eqNum=170&rl=1

    regards,
    George

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


    Why do you think it is the constructor who throw bad_alloc exception? I think the root cause should be in the new operator function.

    Quote Originally Posted by laserlight View Post
    I don't think so, since it is the constructor that threw the exception.

    regards,
    George

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why do you think it is the constructor who throw bad_alloc exception?
    Read your own post again. You asked: 'is the output of my compiler wrong if the following program prints "Other error"?'.

    Clearly, you were not asking about bad_alloc, but about some other exception.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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


    I do not quite understand your points.

    You mentioned bad_alloc is thrown from constructor, but I think it should be thrown from operator new at frist, then constructor re-throw it.

    Anything wrong?

    Quote Originally Posted by laserlight View Post
    Read your own post again. You asked: 'is the output of my compiler wrong if the following program prints "Other error"?'.

    Clearly, you were not asking about bad_alloc, but about some other exception.

    regards,
    George

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You mentioned bad_alloc is thrown from constructor, but I think it should be thrown from operator new at frist, then constructor re-throw it.

    Anything wrong?
    Yes. I never said bad_alloc was thrown from the constructor of A. I said that some exception other than bad_alloc was thrown from the constructor of A.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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


    You mean?

    Code:
    throw 1;
    Quote Originally Posted by laserlight View Post
    Yes. I never said bad_alloc was thrown from the constructor of A. I said that some exception other than bad_alloc was thrown from the constructor of A.

    regards,
    George

Popular pages Recent additions subscribe to a feed