Search code examples
c++qnx

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


I have the following scenario:

#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) 
}

My program always crashes when inserting output to file_os and creates a core dump.

The code works fine in Linux but not in QNX :\

Do you have any explanation? hint?


Solution

  • The problem is that you are using the default constructor of basic_ostream which, by the standard, does not exist. I have no idea why g++ and QCC compile your code successfully, but they shouldn't.

    Anyway, using non standardized functions reveals non standardized behavior, in your case a crash. I don't know if the correct usage of the default constructor is documented anywhere in the gcc docs, but just avoiding it, and using the correct constructor instead, should solve your issue.