0 < negative loop condition bug or misunderstanding on my part

Ary Manzana ary at esperanto.org.ar
Wed Mar 7 18:39:30 PST 2012


On 3/7/12 2:28 AM, Ali Çehreli wrote:
> On 03/06/2012 09:11 PM, ixid wrote:
>  > I'm writing my first basic algorithms, this one is merge sort. This
>  > version throws an exception when array.length - setSize is negative
>  > (which should be fine, the rest of my function would deal with it):
>  >
>  > template mergeSort(T)
>  > {
>  > void mergeSort(ref T[] array, const T setSize = 100)
>  > {
>  > T[][] merge;
>  > merge.length = array.length / setSize;
>  > T ii, jj;
>  > for(ii = 0, jj = 0;ii < array.length - setSize;ii += setSize, ++jj)
>
> We don't know what T is, but I assume a signed type like int.
>
> array.length is size_t, i.e. an unsigned type. Unsigned types have this
> nasty habit of converting the entire expression to unsigned (that is a
> rule since C). So array.length - setSize above is size_t.

Wouldn't it make more sense to convert the entire expression to signed 
if there's at list one signed integer?

I really don't get it...

It's like déjà-vu all the time in this newsgroup.


More information about the Digitalmars-d-learn mailing list