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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:32:27+00:00 2026-06-14T20:32:27+00:00

Two simple questions: In plain C we frequently use xmalloc which is a allocate-or-abort

  • 0

Two simple questions: In plain C we frequently use xmalloc which is a allocate-or-abort routine. I implemented it in C++. Is this a right exception-free implementation?

template <typename T>
T *xnew(const size_t n)
{
    T *p = new (std::nothrow) T[n];
    if (p == nullptr)
    {
        cerr << "Not enough memory\n";
        abort();
    }
    return p;
}

int main()
{
    int *p = xnew<int>(5000000000LL);
}

Second question, if I remove the <int> from the xnew<int>(5000000000LL); call, compiler (g++ 4.7.2) cannot infere that [T = int] anymore although the return type int * is still there. Why is that?

Edit: Isn’t there any overhead when using the new version which could throw exception even if it’s not thrown? I really don’t want to use any exceptions when not absolutely necessary.

  • 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-06-14T20:32:28+00:00Added an answer on June 14, 2026 at 8:32 pm

    I fail to see why this is necessary. new will throw std::bad_alloc
    if it fails to allocate memory. If you don’t handle exceptions, this
    will lead to a call to std::terminate which effectively ends the
    program and has the same behavior as xmalloc.

    Of course, this changes when your compiler does not implement exceptions.

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

Sidebar

Related Questions

This is two questions in one, so here we go. I have a simple
Simple question which I can't seem to find an answer of: I have two
Well this is a simple question i want to filter two elements sorted reverse
I have two simple submit buttons on my form, which work just fine: <button
I'll use this simple query as an example: Select MyColumn From MyTable Where PrimaryKeyColumn
I have two simple questions that concern the exceptions in Symfony2. Here follows the
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
I have a simple but subtle question. Below you see two different declaration variants
This question has two parts (maybe one solution?): Sample vectors from a sparse matrix
Imagine two simple java applications. Both of them are implementing the same JAR file

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.