Reputation: 77
Yesterday I was coding when I found some weird C++ compiler behavior.
This code compiles effortlessly on my computer using g++ 7.2.0:
#include <iostream>
const int SIZE = 1e6;
struct ArrayOfInts
{
int a[SIZE];
} array_of_ints;
int main()
{
std::cout << array_of_ints.a[0];
return 0;
}
However this code is different:
#include <iostream>
#include <functional>
const int SIZE = 1e6;
struct ArrayOfPairs
{
std::pair<int, int> a[SIZE];
} array_of_pairs;
int main()
{
std::cout << array_of_pairs.a[0].first;
return 0;
}
It takes noticeably longer to compile. When looking at Task Manager, I notice that "cc1plus.exe" memory usage jumps to ~500 MB while compiling this snippet of code. Yesterday when I set the size to 1e7, my computer froze.
I don't understand why this happened because the array of pairs only needs ~4 MB of memory, yet it is taking more than 100 times of the memory to compile.
I've tested on some online sites with versions of g++ different from mine and they also take a lot of time to compile.
Of course there are many ways to circumvent this problem, but it feels kind of annoying when you make some mistake in your code and your computer freezes when compiling.
So I want to ask where does the problem originate from? Is it C++ or g++ or my fault? My guess is that it has something to do with std::pair not being a POD type.
Upvotes: 4
Views: 1066
Reputation: 4884
The problem seems to happen in GCC 7.3 (and older) and Clang 3.3 (and older). You can easily test these cases with Godbolt's compile explorer, see: https://godbolt.org/g/1f2WUi
You will see that it compiles effortlessly with GCC 8.1+ and Clang 3.4+, but it fails (timeout) with older versions. They can even handle the SIZE = 1e7
value you mentioned. I have tried some optimization parameters (e.g., -O3) but they don't seem to have an influence on such issue. So maybe you should upgrade your compiler version in order to solve this particular problem.
Upvotes: 1