I have switched from MS Visual Studio to gcc, and currently I am trying to recompile some of the codes I have written in VS by gcc. Now I come across something odd. Simply explained, consider the following code, but first, note that I already know it's a very bad code (which is not the point here)
#include <iostream>
int main()
{
int i = 0,
a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
b[10] = { 6, 5, 4, 1, 3, 2, 9, 7, 10, 8 };
while (i ^ 5) a[i++] = a[i] + b[i];
while (i ^ 10) a[i++] = a[i] - b[i];
for (int j = 0; j < 10; j++)
std::cout << a[j] << ' ';
}
When I compile it with Visual Studio, it results in:
7 7 7 5 8 4 -2 1 -1 2
as expected. With gcc v.4.3.6 I also get the same result (Live example).
But when I switch to gcc 5.3.0, it results in:
7 7 5 8 8 -2 1 -1 2 -4198061
after generating many warnings about undefined behavior.
The question is, why visual studio, even in its most recent version, doesn't care about the quality of code and undefined behaviors, and why earlier versions of gcc do the same? What has happened in recent versions of gcc?