Why does the size of my binary depend on the size of my array?

I recently came across a very strange effect of bloating code, which I really can’t explain to myself ... The following is a working minimal example:

#include <array>

const int SIZE = 4000000;

struct Foo
{
    static Foo& GetInstance()
    {
        static Foo instance;
        return instance;
    }

    std::array<float, SIZE> Bar;
};

int main()
{
    Foo::GetInstance().Bar[0] = 1.0f;
    return 0;
}

The resulting binary (built using GCC MinGW 4.9.2 x86_64 posix sjlj) is 15.28 MB in size . However, if you install, for example, SIZE = 1instead you get a 17 KB binary .

So why does the size of the binary depend on the size of the array here? Apparently, the effect is due to the fact that the structure is Singleton. However, I still don't see any plausible reasoning about why the compiler will inflate a binary file. Thank you for your help!

( -std=c++11. Btw C-...)

+4
1

"-, , Singleton. , , ".

, . singleton . , , , singleton , , .text.

But it all depends on the current compiler implementation and optimization levels.

+6
source

All Articles