Promoting unsigned long int to long int



Hello, I'm trying to sort an array of pointers based on the values
they point to. I'm using the quick sort method. The array of pointers
is parent_vpa and left, right represent the indices. The axis can take
value 0,1,2 and hence i decided to use unsigned char in order to save
some space. I noticed that the program fails and terminates abnormally
when I use left and right as unsigned long but works well when its
data type is declared as long. I would have preferred unsigned long
because the array indices should not be negative and in the calling
function unsigned integers (left = 0, right = n - 1) are passed. I
think the error occurs because of when there is exactly one element in
the array , the j-- can cause j to assume garbage values. My question
is am I breaking any rule or commiting some illegal action by passing
an unsigned long int as a parameter in calling function but using a
long int to receive the variable ? My compiler doesn't complain.

#define ulong unsigned long int
#define uchar unsigned char

void quicksort(vector **parent_vpa, ulong left, ulong right, uchar
axis)
{
ulong i, j;
vector *pivotpoint;
vector *tempstore;

i = left;
j = right;
pivotpoint = parent_vpa[(left+right)/2];

while (i <= j)
{
while (parent_vpa[i]->coord[axis] < pivotpoint->coord[axis])
{
i++;
}
while (parent_vpa[j]->coord[axis] > pivotpoint->coord[axis])
{
j--;
}

if (i <= j)
{
tempstore = parent_vpa[i];
parent_vpa[i] = parent_vpa[j];
parent_vpa[j] = tempstore;
i++;
j--;
}
}

if (left < j)
{
quicksort(parent_vpa, left, j, axis);
}

if (i < right)
{
quicksort(parent_vpa, i, right, axis);
}

return;
}
.



Relevant Pages

  • Re: Promoting unsigned long int to long int
    ... try to access elements of your array that don't exist, ... void quicksort (void quicksort(vector **parent_vpa, ulong left, ulong ... quicksort(parent_vpa, left, j, axis); ...
    (comp.lang.c)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)
  • [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)
  • Re: [RFC][PATCH] flexible array implementation v4
    ... I call it a flexible array. ... so never does an order>0 allocation. ... storage for pointers to the second level. ... all locking must be provided by the caller. ...
    (Linux-Kernel)