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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:21:36+00:00 2026-06-05T15:21:36+00:00

If you use set_new_handler and your handler function is called, is errno guaranteed to

  • 0

If you use set_new_handler and your handler function is called, is errno guaranteed to be set, the way it is on a return of 0 from malloc? Or is it better to use strerror(ENOMEM)? errno works on Microsoft C++ and GCC, but that still leaves the question of whether it’s guaranteed.

  • 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-05T15:21:37+00:00Added an answer on June 5, 2026 at 3:21 pm

    I don’t think errno is good enough for detecting dynamic memory allocation failures. Looking at N3337, specifically 3.7.4.1 Allocation functions:

    2 […] Even if the size of the space requested is zero, the request can fail.
    If the request succeeds, the value returned shall be a non-null pointer value (4.10) p0 different from any
    previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The
    effect of dereferencing a pointer returned as a request for zero size is undefined.35

    3 An allocation function that fails to allocate storage can invoke the
    currently installed new-handler function (18.6.2.3), if any. [ Note: A
    program-supplied allocation function can obtain the address of the
    currently installed new_handler using the std::get_new_handler
    function (18.6.2.4). —end note ] If an allocation function declared
    with a non-throwing exception-specification (15.4) fails to allocate
    storage, it shall return a null pointer. Any other allocation function
    that fails to allocate storage shall indicate failure only by throwing
    an exception of a type that would match a handler (15.3) of type
    std::bad_alloc (18.6.2.1).

    and footnote 35 (this is only a indicative and non-normative):

    35) The intent is to have operator new() implementable by calling std::malloc() or std::calloc(), so the rules are substantially
    the same. C++ differs from C in requiring a zero request to return a non-null pointer.

    Now, heading on to the C standard draft, N1570 and a look at 7.5 Errors <errno.h>:

    3 The value of errno in the initial thread is zero at program startup (the initial value of
    errno in other threads is an indeterminate value), but is never set to zero by any library
    function.202) The value of errno may be set to nonzero by a library function call
    whether or not there is an error, provided the use of errno is not documented in the
    description of the function in this International Standard.

    It appears that errno may be set by a malloc failure but this is not required.

    Also 7.22.3 Memory management functions from the N1570 does not specify that malloc or friends are required to set errno.

    My suggestion would be to stick to what the standard guarantees and use the exception (std::bad_alloc) thrown by new (i.e. not use the no-throw new).

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

Sidebar

Related Questions

C++ programs can define and set a new_handler() that should be called from memory
I use the following code: Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3);
i am new to android development. I am use the following code to set
I'm new to git. I use Windows, and github & bitbucket. I set up
I'm trying to use set! to modify the thread-local bindings of a var --
Can the application use set same char in the XON and XOFF? If yes,
I use to set WithEvents variables to Nothing in Destuctor , because this will
I want to use a set of 2 image layers as a background in
I was trying use a set of filter functions to run the appropriate routine,
Django tutorials everywhere use constant-set application name all around - in urls file, in

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.