5

Why is it that when compiling Sample 1, it uses all my RAM and crashes my computer yet Sample 2 compiles instantly without doing so?

Sample 1:

class Foo
{
    int a = 0;
};

class Test
{
    Foo foo[4000000] = {};
};

int main()
{
    Test t;
}

Sample 2:

class Foo
{
    int a = 0;
};

int main()
{
    Foo foo[4000000] = {};
}

Lastly, is there any way to stop Sample 1 from using tons of RAM when compiling? I'm using gcc version 5.3.0 and I compiled the above with -std=c++11. Note that class Test should only require a mere 16 MB of memory.

For any

Community
  • 1
  • 1
Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42
  • `it uses all my RAM and crashes my computer` If GCC does that, it's obviously a compiler bug. – user6412786 Jun 03 '16 at 00:19
  • Fascinating. I'm going to guess that gcc is attempting to create a constructor for the object by generating the code to initialize each one of the four million instances of `Foo` in `Test`'s class. Dunno why gcc can't do it the same way as it does in the second example. P.S. `class Test` should require 16mb of memory, 4 million * 4 bytes per int. – Sam Varshavchik Jun 03 '16 at 00:29
  • It's not terribly unreasonable to generate unoptimized code and then optimize it. Though, obviously, that yields dramatically suboptimal results in this case. – David Schwartz Jun 03 '16 at 00:37

1 Answers1

-2

This is definitely a bug. I can reproduce this with 5.3 on my system. RAM usage rapidly increases, but I closed the program because I don't want my system to crash. On the other hand, if I compile it in Clang 3.8, it compiles almost instantly.

I suggest reporting this to gcc.gnu.org/bugzilla. As indicated here take a look at bug reports 59659, 68203, and 56671. I'm pretty sure they all point to the same problem of GCC's inability to have a large array of non-trivial class type.

Community
  • 1
  • 1