www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why do static arrays affect executable size?

reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
// 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?
Feb 10 2017
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
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.
Feb 10 2017
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Feb 10 2017
next sibling parent reply sarn <sarn theartofmachinery.com> writes:
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.
Feb 10 2017
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
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.
Feb 11 2017
prev sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
Thanks for the clarifications.
Feb 11 2017