Thread overview
Why do static arrays affect executable size?
Feb 10, 2017
Bastiaan Veelo
Feb 10, 2017
Stefan Koch
Feb 10, 2017
Jonathan M Davis
Feb 11, 2017
sarn
Feb 11, 2017
Bastiaan Veelo
Feb 11, 2017
Bastiaan Veelo
February 10, 2017
// 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?
February 10, 2017
On Friday, 10 February 2017 at 11:21:48 UTC, Bastiaan Veelo 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?

Yes.

February 10, 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

February 11, 2017
On Friday, 10 February 2017 at 15:12:28 UTC, Jonathan M Davis wrote:
> 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++.

An important difference with C/C++ in this case is that D floats are initialised to NaN, not 0.0.  In binary (assuming IEEE floating point), 0.0 has an all-zero representation, but NaNs don't.  Therefore, in C/C++ (on most platforms), default-initialised floats can be allocated in the BSS segment, which doesn't take up executable space, but in D, default-initialised floats have to be put into the compiled binary.

If you explicitly initialise the array to all 0.0, you should see it disappear from the binary.
February 11, 2017
On Saturday, 11 February 2017 at 00:16:04 UTC, sarn wrote:
> If you explicitly initialise the array to all 0.0, you should see it disappear from the binary.

I was actually wondering whether initialisation would make a difference, so thank you for this.

Bastiaan.
February 11, 2017
Thanks for the clarifications.