P
pereges
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->coord[axis] < pivotpoint->coord[axis])
{
i++;
}
while (parent_vpa[j]->coord[axis] > pivotpoint->coord[axis])
{
j--;
}
if (i <= j)
{
tempstore = parent_vpa;
parent_vpa = 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;
}
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->coord[axis] < pivotpoint->coord[axis])
{
i++;
}
while (parent_vpa[j]->coord[axis] > pivotpoint->coord[axis])
{
j--;
}
if (i <= j)
{
tempstore = parent_vpa;
parent_vpa = 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;
}