lundi 23 avril 2018

C++ ofstream segmentation fault only when optimizing

Strange issue. The following block of code works fine on my laptop EXCEPT if I specify "-O1" flag on GCC. In that case I get a Segmentation Fault on the first line

ofstream ofs;
ofs.open("output.txt", ios::out | ios::app);
if (ofs && ofs.is_open()){
    ofs << "test";
    ofs.close();
}

What's even stranger is if I compile with "-O1" on my desktop it works great. If I copy the exe produced by my desktop to my laptop it crashes with the same error. Both computers are running Windows 10 Creator Update. What's going on?

Aucun commentaire:

Enregistrer un commentaire