4

I have been coding for quite a few years now, but have only just recently started getting into C++.

I have already made quite a few programs in it, but have recently started running into some odd behaviour. The cases are simple enough that I expect this to be an error with my environment, and not the language itself, but I have run into a dead end.

Consider this simple program:

#include <iostream>
using namespace std;
int main() {
   cout << "Test" << endl;
   return 0;
}

If I compile that and run it, I get, as expected, "Test", in my console.

Now, if I add a vector to it:

#include <iostream>
#include <vector>
using namespace std;
int main() {
   cout << "Test" << endl;
   vector<double> whatever;
   return 0;
}

For some reason, I do not get any output from that.

I have tried initalizing the vector as an empty vector aswell as with predefined values.

I tried adding a for loop running from 0 to 2^32 to see if the program failed entirely, or if it is just the output. This is where things got even weirder.

At first, I placed the loop before defining the vector; that caused the cout to suddenly work again (i.e. "Test" was printed in the console), aswell as stalling the program as expected. I then moved the for loop to after the vector definition, and then it broke entirely; I received no output, and the program exited almost instantly without error.

The issues persist when I remove the using namespace std; and prefix my cout and vector with std::

I use the g++ v6.3 compiler from MinGW. I am running Windows 10.

I am aware that this problem is probably extremely hard to reproduce, but I'll try my luck here before throwing my computer out the window.

Thanks in advance.

Edit: I fixed the issue by using Cygwin instead of MinGW. I will leave the question open in case someone has encountered a similar issue and has a fix that doesn't involve abandoning MinGW

ItzBenteThePig
  • 696
  • 1
  • 9
  • 19
  • 1
    Recommendation: use gdb to step through the program and see where it is failing. Unless we can reproduce your environment we can't help with this one. – user4581301 Oct 22 '21 at 18:40
  • There's nothing wrong with the code, so it's something weird. Try commenting out the declaration of your array and the #include of vector and see if the output reappears. – Joseph Larson Oct 22 '21 at 18:49
  • @JosephLarson It does indeed reappear. I believe it to be a compiler issue of sorts. Reinstalling did unfortunately not fix the issue. – ItzBenteThePig Oct 22 '21 at 18:50
  • 3
    Take a look at https://stackoverflow.com/questions/54409714/mingw-and-c-vectors And hang in there! Those type of issues can really be frustrating :( – diogoslima Oct 22 '21 at 18:53
  • What an interesting obscure problem! If it is definitely MinGW that is the cause of the problem, maybe the question title and tags should be edited to mention this, to help others with the same problem find it. – saxbophone Oct 23 '21 at 08:03
  • I just tried with `i686-w64-mingw32-g++ (GCC) 10.3.1` and there it worked, so it seems they have fixed it, whatever it was. – Ted Lyngmo Oct 26 '21 at 17:28
  • Apparently MinGW g++ V6.3 had some problems. Your problem should've been caused by some compiler bug. They were fixed in later versions. I tried to run your snippet in Code::Blocks IDE and MinGW g++ V8.1. It worked properly. So, updating compiler should solve the problem. – Abdur Rakib Oct 27 '21 at 11:01
  • I have a similar issue with version on windows 10 with g++ version 12.1. The only difference is, that it doesnt happen when defining, but when doing any push to the vector. (probably the compiler deletes the defining when its unused) No idea how to solve – Kaisky Aug 12 '22 at 23:06

1 Answers1

0

I am running Linux and ran the code, and it ran successfully. So this must be a compiler issue.

Maybe try using a different compiler like Cygwin / Microsoft Windows SDK.

Farouk
  • 261
  • 1
  • 10