Initialisation of static immutable arrays

Denis Koroskin 2korden at gmail.com
Wed Oct 6 05:25:04 PDT 2010


On Wed, 06 Oct 2010 16:21:08 +0400, Lars T. Kyllingstad  
<public at kyllingen.nospamnet> wrote:

> On Wed, 06 Oct 2010 07:39:48 -0400, Steven Schveighoffer wrote:
>
>> On Wed, 06 Oct 2010 06:16:45 -0400, Lars T. Kyllingstad
>> <public at kyllingen.nospamnet> wrote:
>>
>> [...]
>>
>>> 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];
>>>   }
>>
>> Actually, static probably will prevent it from being created every time
>> foo is called.  I don't think there's a way to prevent it from being
>> created every time the program is run.
>
> Does anyone know a way to verify this?  (If it is in fact created every
> time the function runs, I'll change it to a module-level array
> initialised in a 'static this()' instead.)
>

It's static so it allocates only once. But the following:

immutable int[3] = [1, 2, 3];

always allocates, but I see no reason why it should.


More information about the Digitalmars-d-learn mailing list