Classes and templates

Kapps via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 31 13:45:12 PDT 2016


On Monday, 31 October 2016 at 20:25:18 UTC, Hefferman wrote:
>             for (uint k = 1; k < n; k++) {
>                 if (a[k-1] > a[k]) {
>                     T tmp = a[k];
>                     a[k] = a[k+1];
>                     a[k+1] = tmp;
>                     sorted = false;
>                 }
>             }
>             n--;
>         } while (!sorted);
>     }
> }
> [/code]
>
> It gives a Range Violation upon executing, but I guess it's 
> part of the algorithm....

The value of n is array length, so k reaches (n - 1), therefore 
k+1 gives you n which is out of bounds.


More information about the Digitalmars-d mailing list