#include <iostream>
#include <fstream>
int main(int argc, char* argv[])
{
const std::string string_2 = "string_2";
const std::string string_1 = "string_1";
std::cout << string_2 << std::endl;
std::cout << string_1 << std::endl;
std::fstream beep("abcd");
std::cout << string_2 << std::endl;
std::cout << string_1 << std::endl;
printf("%s", string_1.c_str());
}
I am using CMake. When I run this with set(CMAKE_CXX_FLAGS_RELEASE "-O0 -DNDEBUG")
, I get the following bad output:
string_2
string_1
string_2
╘ ±o☻
╘ ±o☻
But when I run it after compiling it with -03
, the problem does not exist.
Clearly, string_1
's value has been changed by the instantiation of an ifstream
.
If I change the code to define string_2
after string_1
, it is instead string_2
that is changed. If I make the strings static, then no change occurs.
So what is clear to me is that the most recently-defined string which is not static is having its value corrupted when the fstream
is instantiated.
Why could this be happening?
I am using CLion 2019.1, using a MinGW environment (mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32)
Using the bundled CMake, and MinGW's bundled g++.