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 3627986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:57:30+00:00 2026-05-18T23:57:30+00:00

I am a student and I have small knowledge on C++, which I try

  • 0

I am a student and I have small knowledge on C++, which I try to expand. This is more of a philosophical question.. I am not trying to implement something.

Since

#include <new> 
//...
T * t = new (std::nothrow) T();
if(t)
{
    //...
}
//...

Will hide the Exception, and since dealing with Exceptions is heavier compared to a simple if(t), why isn’t the normal new T() not considered less good practice, considering we will have to use try-catch() to check if a simple allocation succeeded (and if we don’t, just watch the program die)??

What are the benefits (if any) of the normal new allocation compared to using a nothrow new? Exception’s overhead in that case is insignificant ?

Also, Assume that an allocation fails (eg. no memory exists in the system). Is there anything the program can do in that situation, or just fail gracefully. There is no way to find free memory on the heap, when all is reserved, is there?

Incase an allocation fails, and an std::bad_alloc is thrown, how can we assume that since there is not enough memory to allocate an object (Eg. a new int), there will be enough memory to store an exception ??

Thanks for your time. I hope the question is in line with the rules.

  • 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-18T23:57:31+00:00Added an answer on May 18, 2026 at 11:57 pm

    Since
    dealing with Exceptions is heavier
    compared to a simple if(t), why isn’t
    the normal new T() not considered less
    good practice, considering we will
    have to use try-catch() to check if a
    simple allocation succeeded (and if we
    don’t, just watch the program die)??
    What are the benefits (if any) of the
    normal new allocation compared to
    using a nothrow new? Exception’s
    overhead in that case is insignificant
    ?

    The penalty for using exceptions is indeed very heavy, but (in a decently tuned implementation) the penalty is only paid when an exception is thrown – so the mainline case stays very fast, and there is unlikely to be any measurable performance between the two in your example.

    The advantage of exceptions is that your code is simpler: if allocating several objects you don’t have to do “allocate A; if (A) { allocate B; if (B) etc…”. The cleanup and termination – in both the exception and mainline case – is best handled automatically by RAII (whereas if you’re checking manually you will also have to free manually, which makes it all too easy to leak memory).

    Also, Assume that an allocation fails
    (eg. no memory exists in the system).
    Is there anything the program can do
    in that situation, or just fail
    gracefully. There is no way to find
    free memory on the heap, when all is
    reserved, is there?

    There are many things that it can do, and the best thing to do will depend on the program being written. Failing and exiting (gracefully or otherwise) is certainly one option. Another is to reserve sufficient memory in advance, so that the program can carry on with its functions (perhaps with reduced functionality or performance). It may be able to free up some of its own memory (e.g. if it maintains caches that can be rebuilt when needed). Or (in the case of a server process), the server may refuse to process the current request (or refuse to accept new connections), but stay running so that clients don’t drop their connections, and things can start working again once memory returns. Or in the case of an interactive/GUI application, it might display an error to the user and carry on (allowing them to fix the memory problem and try again – or at least save their work!).

    Incase an allocation fails, and an
    std::bad_alloc is thrown, how can we
    assume that since there is not enough
    memory to allocate an object (Eg. a
    new int), there will be enough memory
    to store an exception ??

    No, usually the standard libraries will ensure, usually by allocating a small amount of memory in advance, that there will be enough memory for an exception to be raised in the event that memory is exhausted.

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

Sidebar

Related Questions

I am a broke college student. I have built a small web app in
Say I have a Student table, it's got an int ID. I have a
I have a list of tables i.e. student, teacher, staff, dept. and so on
Using MS Access 2007, I am creating a student management database. I have tables
I am trying to teach ASP.NET MVC to students (undergrads) that have been studying
Programming Student here...trying to work on a project but I'm stuck. The project is
I'm a Engineering student and I'm attending a Database and Information Systems class this
Let's say I have a table that represents a super class, students . And
I am a student studying software development, and I feel programming, in general, is
I'm a CompSci student, and fairly new at C#, and I was doing a

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.