如何制作一个简单的C++;未刷新std::cout的程序 < >为了更好地理解C++中的缓冲流,我想编写一个简单的程序,其中在终止之前, STD::CUT缓冲区不刷新。因为我已经读到正常终止时会刷新std::cout,所以我尝试抛出一个运行时错误。我还避免使用std::endl,因为我知道这会强制刷新。第一次尝试: //file noflush.cpp #include <iostream> int main() { std::cout << "Don't write me to the console!"; throw 0; }

如何制作一个简单的C++;未刷新std::cout的程序 < >为了更好地理解C++中的缓冲流,我想编写一个简单的程序,其中在终止之前, STD::CUT缓冲区不刷新。因为我已经读到正常终止时会刷新std::cout,所以我尝试抛出一个运行时错误。我还避免使用std::endl,因为我知道这会强制刷新。第一次尝试: //file noflush.cpp #include <iostream> int main() { std::cout << "Don't write me to the console!"; throw 0; },c++,stream,buffer,stdout,cout,C++,Stream,Buffer,Stdout,Cout,即使我强制执行运行时错误,缓冲区似乎仍会在终止时刷新。是否可以在缓冲区中“串入”一些数据,使其不写入设备?通过以下示例,我可以使用gcc 4.8.3创建您想要的行为: #include <iostream> #include <vector> int main() { std::string str; for(unsigned long int i = 0; i < 10000; ++i) str += "Hello ! ";

即使我强制执行运行时错误,缓冲区似乎仍会在终止时刷新。是否可以在缓冲区中“串入”一些数据,使其不写入设备?

通过以下示例,我可以使用gcc 4.8.3创建您想要的行为:

#include <iostream>
#include <vector>

int main()
{
    std::string str;
    for(unsigned long int i = 0; i < 10000; ++i)
        str += "Hello ! ";
    str += "END";
    std::cout << str;

    std::vector<double>* p;
    p->push_back(1.0);
    delete p;

    std::cout << "STILL ALIVE !" << std::endl;

    return 0;
}
#包括
#包括
int main()
{
std::字符串str;
for(无符号长整数i=0;i<10000;++i)
str+=“你好!”;
str+=“结束”;
标准:不能向后推(1.0);
删除p;
STD::CUT< P>这不是标准C++,但是在POSIX中,你可以发送一个“杀死”信号来杀死正在运行的进程。这将停止执行,而不需要清理,比如刷新缓冲区。
<>编辑:我意识到信号不仅仅是POSIX,实际上是C标准库的一部分(并且包含在C++标准库中)。
#包括
// ...

std::cout据我所知,在程序终止之前,没有符合标准且干净的方法来避免
std::cout
flush()
(但是,当然,您可以使用不干净的方法,例如,发出信号或)。根据,std::cout
控制的实际缓冲区类型是实现定义的,但派生自,它似乎不允许以模拟缓冲区无声吞咽的方式进行公共访问


此外,正如我在一篇评论中所指出的,即使是异常的程序终止(通过
std::terminate()
或可能关闭或可能不关闭打开的资源,因此这也是实现定义的。

如果我理解正确,您希望捕获或忽略
std::cout
的输出:

#include <iostream>
#include <sstream>
int main()
{
    // Capture the output to `std::cout`
    {
        std::cout << "[Capture Output]" << std::endl;
        std::stringstream cpature;
        auto restore = std::cout.rdbuf(cpature.rdbuf());

        std::cout << "... captured output ..." << std::endl;

        std::cout.rdbuf(restore);
        std::cout << "[Enable Output]" << std::endl;

        // Display the cpatured output.
        std::cout << cpature.rdbuf();
    }
    std::cout << std::endl;

    // Even more drasticly: Ignore the output to `std::cout`
    {
        std::cout << "[Ignore Output]" << std::endl;
        auto restore = std::cout.rdbuf(nullptr);

        std::cout << "... ignored output ..." << std::endl;

        std::cout.rdbuf(restore);
        std::cout << "[Enable Output]" << std::endl;
    }

    std::cout << "[End]\n";
}
#包括
#包括
int main()
{
//将输出捕获到`std::cout`
{
std::cout
#包括
#包括
#包括
int main()
{
std::stringstream-cpauture;
自动还原=std::cout.rdbuf(cpauture.rdbuf());
标准::cout.rdbuf(恢复);
for(无符号长整数i=0;i<10000;++i)

std::只能说,即使在使用
std::abort()
中止时,缓冲区也会被刷新(使用LLVM libc++的Apple LLVM版本6.0(clang-600.0.57)(基于LLVM 3.5svn),尽管根据它的实现定义是否关闭文件等开放资源(在调用
std::abort()
).Uhmm…该示例在循环中是否使用10而不是10000?当我尝试使用10时,分段错误立即出现。因此,它也可以使用,但更模糊。不,字符串的结尾是
“end”
。它没有被打印。然后字符串被部分打印。缓冲区没有被正确地刷新。我不明白你为什么要放-1!循环是为了创建一个大字符串:最好看到缓冲区出现问题。简单地检查所有字符串的“结束”被打印。“仍然活着”,因为分段错误并不总是被抛出(例如,如果您访问自己的内存,通常是一个不断增长的缓冲区).保持冷静!1和你的评论同时发生,而a-1评论是一个很好的做法。这很正常,我认为是你…@Caduchon,给评论人怀疑的好处是礼貌的。他们毕竟是在试图帮助,不幸的是,这个网站有很多投反对票的忍者,他们不愿意为自己的投票辩护。P.s。很抱歉,我完全偏离了主题。取消引用空指针以引发空指针异常/运行时seg faul如何?这可能是一种很好的跨平台终止进程的方法。@kevinarpe取消引用空指针具有未定义的行为,因此不能保证终止进程,并且在技术上不可移植……但实际上,c空指针取消引用可能会得到更广泛的“支持”这与Caduchon的答案基本相同,只是UB从空指针取消引用可能比UB从取消引用和删除未初始化指针更一致。@kevinarpe在取消引用空指针时崩溃不是保证的。@Leandros我怀疑SIGKILL上不刷新缓冲区也不是问题ranteed。甚至SIGKILL的存在。@immibis它是由SIGKILL的语义保证的。程序无法处理SIGKILL,因此无法运行清理代码来响应它。正确的引语是“全局对象std::cout[…]控制输出到实现定义类型[…]的流缓冲区”(缓冲区而非流对象是实现定义的)
#include <csignal>
// ...
std::cout << "Don't write me to the console!";
std::raise(SIGKILL);
#include <iostream>
#include <sstream>
int main()
{
    // Capture the output to `std::cout`
    {
        std::cout << "[Capture Output]" << std::endl;
        std::stringstream cpature;
        auto restore = std::cout.rdbuf(cpature.rdbuf());

        std::cout << "... captured output ..." << std::endl;

        std::cout.rdbuf(restore);
        std::cout << "[Enable Output]" << std::endl;

        // Display the cpatured output.
        std::cout << cpature.rdbuf();
    }
    std::cout << std::endl;

    // Even more drasticly: Ignore the output to `std::cout`
    {
        std::cout << "[Ignore Output]" << std::endl;
        auto restore = std::cout.rdbuf(nullptr);

        std::cout << "... ignored output ..." << std::endl;

        std::cout.rdbuf(restore);
        std::cout << "[Enable Output]" << std::endl;
    }

    std::cout << "[End]\n";
}
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
    std::stringstream cpature;
    auto restore = std::cout.rdbuf(cpature.rdbuf());    
    std::cout.rdbuf(restore);
    for(unsigned long int i = 0; i < 10000; ++i)
        std::cout <<"Hello ! "  << std::endl;
    std::cout << "END"  << std::endl;

    std::cout << cpature.rdbuf();
    std::vector<double> *p;
    p->push_back(1.0);
    delete p;
    std::cout << "STILL ALIVE !" << std::endl;
}