A new instance of a variable?

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 13 08:06:49 PST 2015


On Friday, 13 November 2015 at 15:49:01 UTC, Ish wrote:
>
> foreach (i; 0..5) {
>   immutable int j = i;
>   etc.
> }
> I want each j to be assigned separate memory so that it can be 
> passed to a calle function and not overwritten by next value of 
> i in foreach()??

Just like in C, you'll have to allocate storage for each object 
you create.

     immutable int* j = new immutable int(i);

Though if you use `new`, you can let the GC free them.

But if the data is immutable, then there's no point in allocating 
separate objects for each number, since they can't be changed.


More information about the Digitalmars-d-learn mailing list