digitalmars.D.learn - Unexpected path of execution
- Charles Hixson (15/15) Oct 19 2021 given this code fragment:
- Adam D Ruppe (17/20) Oct 19 2021 line.length is an unsigned value. Arithmetic on an unsigned thing
- Adam D Ruppe (3/4) Oct 19 2021 BTW this is my personal preference, I have gotten into the habit
- Charles Hixson (7/28) Oct 19 2021 Thank you. That seems to have solved the problem (bar additional
- Dennis (3/8) Oct 19 2021 By the way, if you upgrade to 2.098.0, you get a better error
- Steven Schveighoffer (4/15) Oct 19 2021 context: https://dlang.org/changelog/2.098.0.html#range-error
- Imperatorn (3/19) Oct 19 2021 The day has come! 😍
given this code fragment: if (i < (line.length - 3) ) { writeln ("in c4: i = ", i, ", line.length = ", line.length); add2 (c4, line [i..i+4]); I get this result: in c4: i = 0, line.length = 2 core.exception.RangeError source/freqs.d(32): Range violation ---------------- ??:? _d_arrayboundsp [0x56041325a70d] ??:? _Dmain [0x560413233beb] Why did this get executed? The if test was supposed to prevent this. DMD64 D Compiler v2.097.2 -- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.
Oct 19 2021
On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:given this code fragment: if (i < (line.length - 3) ) in c4: i = 0, line.length = 2line.length is an unsigned value. Arithmetic on an unsigned thing is still unsigned. So UNSIGNED 2 - 3 is not -1, but instead it is size_t.max since it rolls over. Then the comparison also becomes unsigned. So 0 < size_t.max is true, meaning it goes in there. you should be able to fix it if you do if(i < (cast(int) line.length) - 3) to force it to become signed. But this isn't great either. You probably want to change the code to avoid going negative in the first place. Maybe test `i + 3 < line.length` instead. Or maybe `if(!(i > ... wahtever that is)`. You get the idea im brain farting. I personally hate that array.length is unsigned. And I hate that the signed/unsigned mixing prefers unsigned instead of just about anything else. What a pain in the butt. But that's how it is.
Oct 19 2021
On Tuesday, 19 October 2021 at 16:38:50 UTC, Adam D Ruppe wrote:test `i + 3 < line.length` insteadBTW this is my personal preference, I have gotten into the habit of using this style tests with lengths all the time now.
Oct 19 2021
Thank you. That seems to have solved the problem (bar additional testing). And also thanks for your recommendation to add to the index rather than casting the length. It wasn't as "nice" to my eyes at first, but it's a cleaner answer. On 10/19/21 9:38 AM, Adam D Ruppe via Digitalmars-d-learn wrote:On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:-- Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.given this code fragment: if (i < (line.length - 3) ) in c4: i = 0, line.length = 2line.length is an unsigned value. Arithmetic on an unsigned thing is still unsigned. So UNSIGNED 2 - 3 is not -1, but instead it is size_t.max since it rolls over. Then the comparison also becomes unsigned. So 0 < size_t.max is true, meaning it goes in there. you should be able to fix it if you do if(i < (cast(int) line.length) - 3) to force it to become signed. But this isn't great either. You probably want to change the code to avoid going negative in the first place. Maybe test `i + 3 < line.length` instead. Or maybe `if(!(i > ... wahtever that is)`. You get the idea im brain farting. I personally hate that array.length is unsigned. And I hate that the signed/unsigned mixing prefers unsigned instead of just about anything else. What a pain in the butt. But that's how it is.
Oct 19 2021
On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:core.exception.RangeError source/freqs.d(32): Range violation ---------------- ??:? _d_arrayboundsp [0x56041325a70d] ??:? _Dmain [0x560413233beb]DMD64 D Compiler v2.097.2By the way, if you upgrade to 2.098.0, you get a better error message for out of bounds array access.
Oct 19 2021
On 10/19/21 12:49 PM, Dennis wrote:On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:context: https://dlang.org/changelog/2.098.0.html#range-error OMG I've wanted this for so long! Awesome! -Stevecore.exception.RangeError source/freqs.d(32): Range violation ---------------- ??:? _d_arrayboundsp [0x56041325a70d] ??:? _Dmain [0x560413233beb]DMD64 D Compiler v2.097.2By the way, if you upgrade to 2.098.0, you get a better error message for out of bounds array access.
Oct 19 2021
On Tuesday, 19 October 2021 at 17:06:35 UTC, Steven Schveighoffer wrote:On 10/19/21 12:49 PM, Dennis wrote:The day has come! 😍On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:context: https://dlang.org/changelog/2.098.0.html#range-error OMG I've wanted this for so long! Awesome! -Stevecore.exception.RangeError source/freqs.d(32): Range violation ---------------- ??:? _d_arrayboundsp [0x56041325a70d] ??:? _Dmain [0x560413233beb]DMD64 D Compiler v2.097.2By the way, if you upgrade to 2.098.0, you get a better error message for out of bounds array access.
Oct 19 2021