0 votes
in D Programming by
Why Does A Large Static Array Bloat My Executable File Size?

1 Answer

0 votes
by

Given the declaration:

char[1024 * 1024] arr;

the executable size increases by a megabyte in size. In C, this would not as arr would be stored in the BSS segment. In D, arr is not stored in the BSS segment because:

The char type is initialized to 0xFF, not 0. Non-zero data cannot be placed in BSS.

Statically allocated data is placed in thread local storage. The BSS segment is not thread local, and there is no thread local equivalent of BSS.

The following will be placed in BSS:

__gshared byte[1024 * 1024] arr;

as bytes are 0 initialized and __gshared puts it in the global data.

There are similiar issues for float, double, and real static arrays. They are initialized to NaN (Not A Number) values, not 0.

The easiest way to deal with this issue is to allocate the array dynamically at run time rather than statically allocate it.

Related questions

+1 vote
asked Mar 5, 2021 in D Programming by SakshiSharma
0 votes
asked Aug 7, 2023 in Kubernetes K8s by john ganales
...