Re: Promoting unsigned long int to long int



pereges <Broli00@xxxxxxxxx> wrote:
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.

Using unsigned long should be completely ok.

#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;

Lets look at the case you say seems to be the one where things
fail, left = right = 0. In that case i = j = 0

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

Since i and j are equal you get into this loop.

while (i <= j)
{

The next two inner loops shouldn't do anything.

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;

This is useless if i == j.

i++;

Now i gets set to 1.

j--;

And j gets set to ULONG_MAX (unsigned integers "wrap around").
That will keep you in this loop for a loooong time and you will
try to access elements of your array that don't exist, rather
likely resulting in a crash.

}
}

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

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

return;
}

The simplest solution is to just bail out of the function if
left is equal to right - there's nothing to sort in that case.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: ExceptionInInitializerError in Castor
    ... I tried printing the byte array and it prints the exact xml file that is being sent by the application calling this webservice. ... I am running axis on Tomcat 4.1.29 But when I try to unmarshall the byte array, ... Since it seems exceedingly unlikely that one of the Commons Logging's own loggers would fail to implement its Log interface, the most likely scenario is multiple versions of the Commons Logging package in the classpath. ... This sort of thing sometimes happens when an application or package, such as Axis, Castor, or Tomcat comes packaged with various external libraries, such as Commons Logging. ...
    (comp.lang.java.programmer)
  • Kalman Filter
    ... I am creating a algorithm suite for a new virtual IMU design. ... based on an array of gyros and accelerometers for each axis. ...
    (comp.soft-sys.matlab)
  • Re: Crypting the webservice responses in AXIS
    ... original array without problems because on the client side the BASE64 ... Before the BASE64 String is sended back, I am interested to encrypt it ... it is possible in some way to ask AXIS to crypt/decrypt ...
    (comp.lang.java.programmer)
  • Re: Week numbers on X-axis
    ... Was that your question about the number of X axis ticks? ... Store your array in a ... > I have a chart that should display the project status over ... > I save the wanted x-axis in an array and ...
    (microsoft.public.excel.charting)
  • Re: Sorting an Array of String Objects
    ... Doesn't the sort method just change the reference "myArray" to refer to ... if there are other references to the original array other ...
    (comp.lang.java.programmer)