how to initialize immutable 2 dim array

bearophile bearophileHUGS at lycos.com
Sun Oct 31 14:01:42 PDT 2010


Michal Minich:

> I need to use this workaround because global array I want to initialize 
> in module constructor is immutable.

A pointer to a immutable array is immutable. So you aren't improving the code, just making it more obfuscated. So there is zero need to use a pointer.

Immutable data is not meant to be modified, a compiler may move your empty array in read only memory. This is bad.

Some people have recently discussed about this problem and a bug report was proposed. Until some language-supported solution is found, the simpler solution for this problem may be to use a static mutable array...


> is there some performance overhead compared to using  
> for (int x = 0; x <= 255; x++) ?

In theory there is no difference. In practice there is no difference with dmd only if you use the -O compiler switch. So it's a compiler implementation thing, and you need to look at the produced asm if you want to be sure with a different compiler.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list