As the title says, I'm having an issue with ifstream. I was trying to read a text file and that would work fine when debugging but not in the release build. I was able to narrow it down to the example below.
The following code segfaults when it reaches the declaration of file
in foo()
if optimizations are turned on.
#include <iostream>
#include <fstream>
void foo (){
std::cout << "I'm here 3" << std::endl;
std::ifstream file;
std::cout << "I'm here 4" << std::endl;
}
int main()
{
std::cout << "I'm here 1" << std::endl;
std::ifstream file;
std::cout << "I'm here 2" << std::endl;
foo();
return 0;
}
Output:
I'm here 1
I'm here 2
I'm here 3
I'm compiling and linking with the following commands:
g++ -Wall -std=c++17 -O1 main.cpp -o main.exe
If I change the -O1
to O0
it doesn't segfault and runs to the end. I'm not really sure what the issue is.
My system is Windows 10 and I'm running g++.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
. Is this a compiler bug or am I just doing something stupid?