12

I wrote a very small program:

#include <array>
#include <tuple>
#include <iostream>

const unsigned int NUM = 500;

void simple()
{
    using namespace std;
    array<tuple<float, float, float>, NUM> vectors;
}

int main(int argc, char **argv) 
{
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

I compiled it with g++ -std=c++0x.

This version works just fine, but if I increase NUM to maybe 50,000,000, g++ uses 90% CPU and my system freezes entirely.

I understand that a program can crash during execution if there is not enough stack memory. But why would the compiler freeze during compilation?

Is this a bug in g++ or does the compiler need to allocate stack memory during compilation for some reason?

Felix Glas
  • 15,065
  • 7
  • 53
  • 82
Andreas T
  • 733
  • 7
  • 22
  • I think it's because you went over the range for int. – drum Jul 15 '14 at 18:23
  • 9
    Compile to assembler (-S) with `NUM=500`. Do it again with `NUM=5000`, and compare the files. What changed? If there is 10x as much tuple default constructor code, the answer in your case is that g++ is busy emitting 50000000 (probably inlined) default constructors. – Useless Jul 15 '14 at 18:24
  • Interesting. What version of g++? – jaredready Jul 15 '14 at 18:25
  • @drum `std::size_t` is an unsigned integer so it would be small again. – dari Jul 15 '14 at 18:26
  • 4
    @drum, 50,000,000 is well within the range of int – jaredready Jul 15 '14 at 18:27
  • Thanks for the info. I miscounted the 0s. – drum Jul 15 '14 at 18:28
  • 2
    OK, I tried it myself - changing `NUM` makes a trivial change to the code (just a different offset to the stack pointer and a different argument to memset, as you'd hope), but a non-trivial difference to compile time. Perhaps it's generating lots of code and then optimizing it out ... – Useless Jul 15 '14 at 18:33
  • If it helps: On Windows, with MinGW, g++ 4.6.2, the compiler crashes with the following error: `cc1plus.exe: out of memory allocating 65536 bytes` – Holt Jul 15 '14 at 18:34
  • @Useless why don't you make that an answer? – Shafik Yaghmour Jul 15 '14 at 18:37
  • Because I don't really have an answer yet ... – Useless Jul 15 '14 at 18:38
  • 10
    This appears to be [GCC bug# 59659](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59659) which should be fixed in 4.9. – Casey Jul 15 '14 at 18:39
  • Now _that's_ an answer. – Useless Jul 15 '14 at 18:40
  • [Compiles and runs fine on gcc 4.9](http://coliru.stacked-crooked.com/a/97575ad4bb46c88f), so it appears that the problem is in fact the cited bug. – Casey Jul 15 '14 at 18:48
  • Thank you, I will install gcc 4.9 as soon as possible and will hopefully be able to continue with my work. – Andreas T Jul 15 '14 at 20:11

0 Answers0