Initialisation of static immutable arrays

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Wed Oct 6 03:16:45 PDT 2010

I have a program that uses an immutable array, the contents of which are 
known at compile time.  Thus, ideally, I want it to be placed in 
the .rodata segment of the program.

Firstly, I seem to remember reading that using an array literal in D will 
always result in a heap allocation.  Is this correct?

Secondly, if the above is not true, how can I verify that the array in 
the following piece of code isn't allocated and/or copied anew every time 
the program runs, or even worse, every time foo() is called?

  void foo()
      static immutable int[3] = [1, 2, 3];

I know, RTFAsm, but some help with that would be appreciated. ;)

Thirdly, intuition tells me that when the array is immutable, the 
'static' shouldn't have an effect.  But experiments (i.e. printing the 
adress of the array) indicate that it does.  Is there some reason for 
this, or is it just a shortcoming of the compiler?


More information about the Digitalmars-d-learn mailing list