Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:59:16+00:00 2026-05-27T04:59:16+00:00

The new expression in the try block throws a bad_alloc exception in my computer.

  • 0

The new expression in the try block throws a bad_allocexception in my computer.

Note that the catch clause receives an exception object by value, not by reference. How come e.what() prints "bad allocation" ? I thought it would be sliced.

#include <iostream>

int main()
{
    try
    {
        int* p = new int[0x1F000000];
    }
    catch(std::exception e)
    {
        std::cout << e.what() << std::endl;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:59:16+00:00Added an answer on May 27, 2026 at 4:59 am

    Visual Studio (Dinkumware?) uses an implementation of std::exception that contains internal storage† for the message. (Complete with a non-standard constructor that accepts a string.)

    Because of this, no virtual dispatch is actually needed to get the error message, it survives any slicing.

    A more orthodox implementation would indeed print a generic exception message, because the derived object was sliced off. (Effectively, MS has made std::exception and std::runtime_error equivalent. There’s nothing wrong with this, since the return value of std::exception::what is implementation-defined, but it explains your results.)


    †Internal storage here is used loosely. It doesn’t have an internal buffer, but it has a const char* and a bool. The const char* points to the message (the return value of what()), and the bool is a flag determining if the buffer should be deleted.

    It’s like this:

    class msvc_exception // for exposition
    {
    public:
        msvc_exception(const char* msg) :
        mMsg(msg),
        mDoDelete(false)
        {}
    
        msvc_exception(const std::string& msg) :
        mMsg(copy_string(msg)),
        mDoDelete(true)
        {}
    
        virtual ~msvc_exception()
        {
            if (mDoDelete)
                delete [] mMsg;
        }
    
        virtual const char* what() const throw()
        {
            return mMsg ? mMsg : "unknown";
        }
    
    private:
        const char* copy_string(const std::string& str)
        {
            const char* result = new char[str.size() + 1];
    
            std::copy(str.begin(), str.end(), result);
            result[str.size()] = 0; // null-terminate
    
            return result;
        }
    };
    

    You see now that bad_alloc works like this:

        class msvc_bad_alloc : // for exposition
            public msvc_exception
        {
        public:
            msvc_bad_alloc() :
            msvc_exception("bad_alloc") // note: a static string, no dynamic storage
            {}
        };
    

    Slicing doesn’t affect the message because the message “exists” in the base class.

    Other compilers, like GCC and LLVM, implement it a bit more straight-forwardly:

    class orthodox_exception
    {
    public:
        orthodox_exception(){}
        virtual ~orthodox_exception() {}
    
        virtual const char* what() const throw()
        {
            return "orthodox_exception";
        }
    };
    
    class orthodox_bad_alloc :
        public orthodox_exception
    {
    public:
        const char* what() const throw()
        {
            return "orthodox_bad_alloc";
        }
    };
    

    Here, slicing would affect your outcome. (That said, after all this: always catch by reference.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Access 2007 is telling me that my new expression is to complex. It used
try { string s = null; s.PadLeft(10); } catch (Exception ex) { // send
I'm new to regular expression and just can't seem to figure this out: '/^[A-Za-z0-9](?:.[A-Za-z0-9]+)$/'
So I'm new to both Joomla and Expression Engine, and want to know if
I'm using Expression Blend 3 I click on add live datasource, add new xml
Why does this lambda expression not compile? Action a = () => throw new
I used the following Regex RE = new Regex(@'?([(\.\//\s\;\,\:\.\)]+)'?); to split the expression which
I have a very complicated mathematica expression that I'd like to simplify by using
I am learning lambda expression and delegates.While i try to execute the following ,I
When I try to validate XML file with a XSD schema containing regular expression,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.