On Sat, 13 Nov 2010 15:57:50 -0500, spir <denis.s...@gmail.com> wrote:

On Fri, 12 Nov 2010 08:03:26 -0500
"Steven Schveighoffer" <schvei...@yahoo.com> wrote:

Essentially, if you change your line above to:

this.patterns = patterns.dup;

Works, but I don't understand why: isn't the duplicate also allocated on the stack? I mean, dup is supposed to just duplicate, isn't it? what does it give to the new structure that the original one hasn't? I thought I would have to do for instance:
        ints[] x;
        void f(int[] ints...) {
          x.length = ints.length;   // allocate new area on the heap
          x[] = ints[];             // copy elements in there
        }


When doing this:

arr1 = arr2;

It copies the reference to the data, not the data itself. Since the data is on the stack, you are still pointing at the stack

arr1 = arr2.dup;

copies the data itself, then returns a reference to the new data. The reference is stored wherever the reference is stored. In your case, the reference this.patterns is stored in the class, so it's also stored on the heap.

I hope this is clearer.

-Steve

Reply via email to