mercredi 13 mai 2015

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

I've recently stumbled upon a very strange code bloat effect that I really cannot 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 with GCC MinGW 4.9.2 x86_64 posix sjlj) is 15.28 MB in size. However if you set e.g. SIZE = 1 you get a 17 KB binary instead.

So why does the size of the binary depend on the size of the array here? Apparently, the effect is caused by the struct being a Singleton. Yet I still don't see any plausible reasoning why the compiler would bloat the binary. Thanks for your help!

(Tested with and without optimization and with the -std=c++11 flag only. Btw this also happens with C-style arrays...)

Aucun commentaire:

Enregistrer un commentaire