On Fri, 12 Nov 2010 08:03:26 -0500 "Steven Schveighoffer" <schvei...@yahoo.com> wrote:
> Pelle, I spent all this time helping him, and you swoop in with the answer > :) > > Yes, he is right, you need to dup the patterns argument. It is something > I very recently discovered. Wow, first thought dup did not solve the issue, but it fact I had introduced another bug in the meanyrime ;-) All works fine, now. I'll be able to restart working on this. D soon has a prototype for a PEG lib. > Here is what happens: > > void foo(int[] arg...) {} > > foo(1,2,3); > > What the compiler does is push 1, 2, and 3 onto the stack, then passes in > a dynamic array reference to that stack data. It does this because heap > allocations are much more expensive than stack allocations. When you just > "save" the data, it is no longer valid. The reason the code all works > within the List constructor is because that is the stack frame where the > array is pushed onto the stack. Right. > Note, you can do foo([1,2,3]), and you will be wasting time duping, but I > have found a solution to that, overloading: > > void foo(int[] arg) {} > > If you pass in an array, then the second overload is used, if you pass in > individual arguments the first overload is used. All right. I find a bit strange that the compiler accepts f([1,2,3]) when the declaration reads eg void f(int[] ints...). Anyway, it cannot harm, I guess. > 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 } > All is well, you should be good. yo! > -Steve Thank you and Pelle for your help, Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com