D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21136 - N sized array takes kN bytes in executable file
Summary: N sized array takes kN bytes in executable file
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P3 blocker
Assignee: No Owner
URL: http://dlang.org/
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-08 07:43 UTC by Raoof Hajibagheran
Modified: 2022-03-25 11:21 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Raoof Hajibagheran 2020-08-08 07:43:23 UTC
hi

```
/* 
dmd -betterC $0
exit 1
*/

//byte[1000000] arr;  // output size: 16664
//bool[1000000] arr;  // output size: 16664
//int[1000000] arr;   // output size: 16664
//char[1000000] arr;  // output size: 1016096
//float[1000000] arr; // output size: 4014368
//double[1000000] arr;// output size: 8016160

struct S {
  double[1000000] arr;// output size: 8016056
}

extern (C) int main() {
  return 42;
}
```
Comment 1 KytoDragon 2020-08-08 09:31:46 UTC
These sizes all look correct.
byte, bool and int initialize to zero, so they don't need to be explicitly stored in the resulting executable. 1000000 chars need 1000000 bytes, floats use 4 bytes each so need 4000000 bytes total and double use 8 bytes each, resulting in a total of 8000000 bytes.
The remaining ~16k bytes are just the code and metadata of the executable.
Comment 2 Raoof Hajibagheran 2020-08-08 15:19:51 UTC
sorry to bother but how can I prevent float and double and char arrays from appearing in the output?
Comment 3 KytoDragon 2020-08-08 15:23:15 UTC
You can have them be zero initialized by assigning zero to the array:

double[1000000] = 0;

Do note that this only decreases the size of the executable. Once the program is loaded into memory these array will obviously be expanded to their full size.
Comment 4 Raoof Hajibagheran 2020-08-08 15:36:42 UTC
I know that but it doesn't work. my assumption was that when you don't define default values for variables the compiler doesn't put them in the output file. but in this case it doesn't work whether you init to zero or not.
Comment 5 Raoof Hajibagheran 2020-08-08 15:40:40 UTC
and another issue is that I'm only defining struct S and never use it but it takes space anyway. but this is a minor issue for me.
Comment 6 Raoof Hajibagheran 2020-08-11 14:18:27 UTC
setting variables to void avoid this problem.
but the output file size is still large I don't know why, I set all the variables to void.
Comment 7 RazvanN 2022-03-25 11:21:21 UTC
Since D initializes all variables by default, it needs to store the initializer. Yes, void initialization is used to not store the initializer. This is all according to the spec, there is no bug here.