Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 10, 2017 Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
// 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 Re: Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | 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 Re: Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | 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 Re: Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sarn | 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 Re: Why do static arrays affect executable size? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Thanks for the clarifications. |
Copyright © 1999-2021 by the D Language Foundation