在 QNX 中为 std::basic_ostream 实例化插入输出时出现内存错误

Memory fault when inserting output for an std::basic_ostream instantiation in QNX

我有以下场景:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <ostream>

class File_ostream final : public std::basic_ostream<char, std::char_traits<char>>
{
};

int main()
{
    const std::string input_file{"file_tests/test.txt.gz"};
    std::ifstream ifs{input_file, std::ios_base::in | std::ios_base::binary};

    File_ostream file_os{};

    file_os << ifs.rdbuf(); // Memory fault (core dumped) 
}

我的程序总是在向 file_os 插入输出并创建核心转储时崩溃。

代码在 Linux 中运行良好,但在 QNX 中运行不正常:\

你有什么解释吗?提示?

问题是您正在使用 basic_ostream 的默认构造函数,按照标准,它不存在。我不知道为什么 g++ 和 QCC 能成功编译你的代码,但他们不应该。

无论如何,使用非标准化函数会揭示非标准化行为,在您的情况下是崩溃。我不知道 gcc 文档中的任何地方是否记录了默认构造函数的正确用法,但只是避免它,而是使用 the correct constructor 应该可以解决你的问题。