ranges reading garbage

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Feb 15 10:43:34 PST 2015


John Colvin:

> prints things like [0, 4, 5, 1, 1, 1459971595, 1459971596, 2, 
> 2, 1459971596, 1459971597, 3, 4, 8, 9, 5, 5, 4441427819, 
> 4441427820, 6, 6, 4441427820, 4441427821, 7] but the output 
> isn't consistent, the big numbers change on each run.

Try to replace the only() with:

[y, y+ys.length, y+ys.length+1, y+1]

Like this:


import std.range, std.algorithm, std.stdio;

void foo(in float[] data, in float[] xs, in float[] ys) @safe {
     iota(0, data.length, ys.length)
     .map!(xBase => iota(xBase, xBase + ys.length - 1)
                    .map!(y => [y, y+ys.length, y+ys.length+1, 
y+1])
                    .joiner)
     .joiner
     .writeln;
}

void main() {
     foo([1,2,3,4,5,6,7,8], [0.1,0.2], [10,20,30,40]);
}



In Rust the compiler enforces that all stack-allocated data 
doesn't come from dead stack frames. In D you have to be careful 
to avoid doing it. In future this kind of bugs will be hopefully 
avoided by a better tracking of the memory.

I am not sure if http://wiki.dlang.org/DIP69 is able to avoid 
this bug, if it can't, then DIP69 needs to be improved.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list