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

  • Home
  • SEARCH
  • 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 6874007
In Process

The Archive Base Latest Questions

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

Recently I ran the following code on ideone.com (gcc-4.3.4) #include <stddef.h> #include <stdio.h> #include

  • 0

Recently I ran the following code on ideone.com (gcc-4.3.4)

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <new>

using namespace std;

void* operator new( size_t size ) throw(std::bad_alloc)
{
     void* ptr = malloc( 2 * 1024 * 1024 * 1024);
     printf( "%p\n", ptr );
     return ptr;
}

void operator delete( void* ptr )
{
    free( ptr );
}

int main()
{
    char* ptr = new char;
    if( ptr == 0 ) {
        printf( "unreachable\n" );
    }
    delete ptr;
}

and got this output:

(nil)
unreachable

although new should never return a null pointer and so the caller can count on that and the compiler could have eliminated the ptr == 0 check and treat dependent code as unreachable.

Why would the compiler not eliminate that code? Is it just a missed optimization or is there some other reason for that?

  • 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:08:29+00:00Added an answer on May 27, 2026 at 4:08 am

    I think you’re expecting way too much of the optimizer. By the time the optimizer gets to this code, it considers new char to be just another function call whose return value is stored on the stack. So it doesn’t see the if condition as deserving special treatment.

    This is probably triggered by the fact that you overrode operator new, and it’s beyond the optimizer’s pay grade to look in there, see you called malloc, which can return NULL, and decide that this overridden version won’t return NULL. malloc looks like Just Another Function Call. Who knows? You might be linking in your own version of that, too.

    There are a couple other examples of overridden operators changing their behavior in C++: operator &&, operator ||, and operator ,. Each of these has a special behavior when not overridden, but behave like standard operators when overridden. For example, operator && will not even compute its right hand side at all if the left hand side evaluates as false. However, if overridden, both sides of the operator && are computed before passing them to operator &&; the short-circuit feature goes away completely. (This is done to support using operator overloading to define mini-languages in C++; for one example of this, see the Boost Spirit library.)

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

Sidebar

Related Questions

I recently ran across some 3rd party C# code which does the following: public
I recently ran into a problem with a COM object that was using a
I recently ran into a problem caused by using fstream::eof(). I read the following
I recently ran across a set of code which instantiated local maps as following:
Recently while trying to answer a questions here, I ran some test code to
I recently ran across some code which the person did the first one. Would
Recently, I ran some of my JavaScript code through Crockford's JSLint , and it
I recently ran across the following snippet in an existing codebase I'm working on
I recently endeavoured to learn about multiple threading, and ran into the following unexpected
I recently ran the database repair tool as per this link: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/db-repair-tool At the

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.