Why do static arrays affect executable size?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 10 07:12:28 PST 2017


On Friday, February 10, 2017 11:21:48 Bastiaan Veelo via Digitalmars-d-learn 
wrote:
> // enum int maxarray = 0;
> enum int maxarray = 2_000_000;
>
> double[maxarray] a, b, c, d;
>
> void main() {}
>
>
> Compiled using "dub build --arch=x86_64 --build=release" on
> Windows (DMD32 D Compiler v2.073.0), the exe size is 302_592
> bytes v.s. 64_302_592 bytes, depending on the array length.
>
> Is that normal?

Module-level and static variables all get put in the executable. So,
declaring a static array like that is going to take up space. A dynamic
array would do the same thing if you gave it a value of that size. The same
thing happens with global and static variables in C/C++.

Similarly, even with a local variable that's a static or dynamic array, if
you use a literal to initialize it, that literal has to be put in the
executable, increasing its size. But the nature of module-level or global
variables is such that even if they're not explicitly assigned a value, they
take up space.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list