相关疑难解决方法(0)

为什么std :: cout输出在发送NULL后完全消失

我花了一段时间才弄清楚为什么一些cout输出似乎消失在以太中.罪魁祸首:

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;
Run Code Online (Sandbox Code Playgroud)

上面代码段的输出将是:

This line shows up just fine
Let's check the value of some_string:     
Run Code Online (Sandbox Code Playgroud)

因此将NULL输入cout将禁用所有输出.为什么?以及如何解决它?

这不会一直发生 - 具有相同代码的同事获得所有预期的输出.如果你想知道为什么我不能阻止使用if语句将NULL输入cout:我在大型代码库中工作,并且不知道这会发生在哪里!我所知道的是我从未出现过的cout陈述.


更多信息:

a_function_that_returns_null()实际上是getenv("HOST").我检查了命令行echo $HOST,因为HOST变量是空的.如果我这样做export HOST=(bash风味),输出就在那里.我不知道HOST变量最初包含什么,也不知道getenv在修改HOST变量之前最初返回的内容; 我所知道的(some_string == 0)都是真的.

c++ gcc

21
推荐指数
2
解决办法
7129
查看次数

标签 统计

c++ ×1

gcc ×1