www.digitalmars.com         C & C++   DMDScript  

D - Array and struct literal implementation idea

An array literal could use the stack in the current scope, so:
[3, 4, 5]
would be like:
int[3] arr = [3, 4, 5]

It brings up the problem with other code keeping references of
data it doesn't own, such as:
class IntHolder
{ 
 int[] hold; 
 this(int[] hold) { this.hold = hold; } 
} 
IntHolder ih = new IntHolder([2, 3, ival]); 

As soon as the caller returns, ih.hold is invalid. Perhaps I should have dup'd
it, but shouldn't it be up to IntHolder if it wants its own copy? The
convention for arrays (and strings) should be copy on write, and copy on save.
That should be the case if array literals is implemented like this or not,
because the data could be anywhere, like temporary shared memory. It's
different for functions that return memory, because it's being given to the
caller, so the memory should exist as long as the function is valid. This could
also be how struct literals are managed.


--
Christopher E. Miller
Apr 24 2004