Promoting unsigned long int to long int
- From: pereges <Broli00@xxxxxxxxx>
- Date: Mon, 30 Jun 2008 03:21:25 -0700 (PDT)
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;
}
.
- Follow-Ups:
- Re: Promoting unsigned long int to long int
- From: Keith Thompson
- Re: Promoting unsigned long int to long int
- From: Jens Thoms Toerring
- Re: Promoting unsigned long int to long int
- From: pete
- Re: Promoting unsigned long int to long int
- Prev by Date: Re: Storing input into a character array
- Next by Date: Re: adding colums to text
- Previous by thread: Runtime stack allocation
- Next by thread: Re: Promoting unsigned long int to long int
- Index(es):
Relevant Pages
|