Tried C++ to D. Wrong result.

Ali Çehreli acehreli at yahoo.com
Mon Nov 27 18:40:41 UTC 2017


On 11/27/2017 10:25 AM, Dmitry wrote:
 > On Monday, 27 November 2017 at 17:21:05 UTC, Dmitry wrote:
 >> It fixed a delay (you can see it on video I posted before), but result
 >> is same.
 >
 > It seems I found the problem.
 >
 > C++ version (line 93):
 > if (image[index + 3] != 0)
 >
 > I changed to
 > if (image[index] != 0)
 >
 > and it works.
 >
 > I don't understand why there was used "+ 3" (and why it works in C++
 > version).

So, it looks like the original code was accessing out of bounds and 
probably that's why you inserted the ((index + 3) < N) check in the D 
version because D was catching that error at runtime.

// C++
     if (image[index + 3] != 0)

// D
     if (((index + 3) < N) && (data[index + 3] != 0))

Which of course would skip the body of the if block, causing a 
difference from the original result.

Ali



More information about the Digitalmars-d-learn mailing list