segfault
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 15 07:48:40 PST 2010
On Sat, 13 Nov 2010 15:57:50 -0500, spir <denis.spir at gmail.com> wrote:
> On Fri, 12 Nov 2010 08:03:26 -0500
> "Steven Schveighoffer" <schveiguy at 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
More information about the Digitalmars-d-learn
mailing list