Thread overview
Why I'm getting this "Range Violation" error?
Jul 09, 2021
rempas
Jul 09, 2021
Mike Parker
Jul 09, 2021
rempas
Jul 09, 2021
zjh
Jul 09, 2021
rempas
Jul 09, 2021
Ali Çehreli
Jul 09, 2021
rempas
July 09, 2021

I have the following code:

import core.stdc.stdio;

void print(T)(string prompt, T args...) {
  size_t len = prompt.length;
  size_t i = 0;

  while (prompt[i] != '{' && i < len) {
    printf("%c", prompt[i]);
    i++;
  }
}

void main() {
  print("Hello, world!\n", 10);
}

When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...

July 09, 2021

On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:

>

When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...

Because you're indexing the prompt with i before you ensure that i is valid. Swap the operands in your if condition:

while (i < len && prompt[i] != '{')

Or better yet, use foreach, which exists to avoid this sort of mistake:

foreach(c; prompt) {
   if(c != '{') printf("%c", c);
}
July 09, 2021

On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:

>

I have the following code:

prompt[i]!='{',here,i=len.so array overflow

July 09, 2021

On Friday, 9 July 2021 at 07:38:50 UTC, Mike Parker wrote:

>

On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:

>

When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...

Because you're indexing the prompt with i before you ensure that i is valid. Swap the operands in your if condition:

while (i < len && prompt[i] != '{')

Or better yet, use foreach, which exists to avoid this sort of mistake:

foreach(c; prompt) {
   if(c != '{') printf("%c", c);
}

OMG!!! Yes! I feel bad for not catching it now... Meanwhile I realized "while" will not work in my example anyway. Thanks for the man! Have a nice day!

July 09, 2021

On Friday, 9 July 2021 at 07:54:44 UTC, zjh wrote:

>

On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:

>

I have the following code:

prompt[i]!='{',here,i=len.so array overflow

Thanks a lot. It seems I have a lot to learn. Have an amazing day!

July 09, 2021
On 7/9/21 12:21 AM, rempas wrote:

>    while (prompt[i] != '{' && i < len) {

In addition to what others said, you can take advantage of ranges to separate concerns of filtering and iteration. Here are two ways:

import core.stdc.stdio;
import std.algorithm;

// Same as your original
void print(T)(string prompt, T args...) {
  prompt
    .filter!(c => c != '{')
    .each!(c => printf("%c", c));

  // If you are not familiar with the shorthand lambda syntax,
  // I read c => foo(c) as "given c, produce foo(c)."
  //
  // Well... 'each' is different because it does not
  // produce anything. It its case: "given c, do this."
}

// This one dispatches filtering to another function but
// still uses a 'foreach' loop
void print2(T)(string prompt, T args...) {
  foreach (c; prompt.sansCurly) {
    printf("%c", c);
  }
}

// I used R instead of 'string' in case it will be more useful
auto sansCurly(R)(R range) {
  return range.filter!(c => c != '{');
}

void main() {
  print("Hello, {world!\n", 10);
  print2("Hello, {world!\n", 10);
}

Ali

July 09, 2021
On Friday, 9 July 2021 at 11:56:40 UTC, Ali Çehreli wrote:
> In addition to what others said, you can take advantage of ranges to separate concerns of filtering and iteration. Here are two ways:
>
> [...]

Of course another help from the legend himself ;) As always thanks a lot and have an amazing day!