Bug 21136 – N sized array takes kN bytes in executable file

Status
RESOLVED
Resolution
INVALID
Severity
blocker
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-08-08T07:43:23Z
Last change time
2022-03-25T11:21:21Z
Assigned to
No Owner
Creator
Raoof Hajibagheran

Comments

Comment #0 by raoofha — 2020-08-08T07:43:23Z
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 by kytodragon — 2020-08-08T09:31:46Z
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 by raoofha — 2020-08-08T15:19:51Z
sorry to bother but how can I prevent float and double and char arrays from appearing in the output?
Comment #3 by kytodragon — 2020-08-08T15:23:15Z
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 by raoofha — 2020-08-08T15:36:42Z
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 by raoofha — 2020-08-08T15:40:40Z
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 by raoofha — 2020-08-11T14:18:27Z
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 by razvan.nitu1305 — 2022-03-25T11:21:21Z
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.