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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:58:44+00:00 2026-05-24T12:58:44+00:00

It took me a while to figure out why some cout output seem to

  • 0

It took me a while to figure out why some cout output seem to disappear into the ether. The culprit:

std::cout<< "This line shows up just fine" << std::endl;
const char* some_string = a_function_that_returns_null();
if (some_string == 0)
    std::cout<< "Let's check the value of some_string: " << some_string << std::endl;

std::cout<< "This line and any cout output afterwards will not show up" << std::endl;

The output from the snippet above will be:

This line shows up just fine
Let's check the value of some_string:     

So feeding a NULL into cout will disable all output afterward. Why? And how to fix it?

This doesn’t happen all the time – a co-worker with the same code gets all the expected output. And in case you wonder why I can’t just prevent feeding NULL into cout with a if statement: I’m working in a large codebase, and don’t know where else this happens! All I know is the cout statements I put never showed up.


More info:

a_function_that_returns_null() is actually getenv("HOST"). I checked on the commandline via echo $HOST that the HOST variable is empty. If I do export HOST= (bash flavor), the output are all there. I have no idea what the HOST variable contains initially nor what getenv returns initially when before I modify the HOST variable; all I know is (some_string == 0) is true.

  • 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-24T12:58:45+00:00Added an answer on May 24, 2026 at 12:58 pm
    const char* some_string = a_function_that_returns_null();
    

    Do you mean that it literally returns a null pointer?

    [2003: 27.6.2.5.4]:

    template<class traits>
    basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out, const char* s);
    

    3. Requires: s is non-null.

    Then streaming some_string is Undefined Behaviour; you cannot dereference a pointer to get a string — even an empty one — if the pointer is not valid.


    This doesn’t happen all the time – a co-worker with the same code gets all the expected output

    UB leads to unreliable symptoms. That you don’t always get a crash can be slightly surprising because most modern OSs make a point of always SIGSEGVing when you try to dereference a null pointer.

    However, from a C++ point of view, anything can happen; in your particular case, your standard library implementation may well be checking for a null pointer and setting an error flag on the stream instead of attempting to dereference the pointer. That is its prerogative.

    (It’s also probably why your subsequent stream operations are failing: attempting to write to a stream does nothing when there’s an error flag set.)

    For example, the libstdc++ that ships with GCC 4.6.0, despite naming s != 0 as a precondition, does do this:

    00325       if (!__s)
    00326     __out.setstate(ios_base::badbit);
    00327       else
    

    However, you must not rely on this behaviour; it could change at any time!


    So, simply don’t do this. Stream a valid, but empty, string if you really must.

    And what’s wrong with std::string?

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

Sidebar

Related Questions

This took me a while to figure out and I found the answer on
Took me a while to figure this out. So, I am answering my own
I'm sharing this as it took me A while to figure out. This is
I just ran across a problem that took a little while to figure out
Okay, it took me a little while to narrow down this problem, but it
Took me a while to find this bug in my code. Trying to create
I can't figure out why this won't read from my file... #include <iostream> #include
While the following code is saving the image I took from my application into
I need to figure out how long some code is taking in Velocity (VTL).
I have the following text from an academic course I took a while ago

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.