I have a bug in a program (no it is not an assignment; I am working on my own this term) that uses unsigned ints.
Without going into the gory details (it's a large program), I need to take the factorial of an expression n - r. Of course, I want to test to be sure that I don't feed my factorial function a negative.
Both n and r are of type unsigned long int.
According the Harbison and Steele (5th edition):
In my program, the problem happens when n is less than r, say 3 an 5 respectively. This means that the difference n - r is negative, and by the above is converted to an unsigned integer, which results in my factorial function being sent an enormous number which invariably crashes the program. Am I thinking correctly here?
I do not wish to be given the solution to this bug; I merely want to know if I am on the right track in finding it.
Xcode project is attached.
Thanks!
Without going into the gory details (it's a large program), I need to take the factorial of an expression n - r. Of course, I want to test to be sure that I don't feed my factorial function a negative.
Code:
if ( n - r > 0 )
{...}
Both n and r are of type unsigned long int.
According the Harbison and Steele (5th edition):
These conversions can be surprising. For example, because unsigned integers are always non-negative, you would expect that the following test would always be true:
Code:unsigned int u; ... if ( u > -1 ) ...
However, it is always false! the (signed) -1 is converted to an unsigned integer before the comparison, yielding the largest unsigned integer, and the value of u cannot be greater than that integer.
In my program, the problem happens when n is less than r, say 3 an 5 respectively. This means that the difference n - r is negative, and by the above is converted to an unsigned integer, which results in my factorial function being sent an enormous number which invariably crashes the program. Am I thinking correctly here?
I do not wish to be given the solution to this bug; I merely want to know if I am on the right track in finding it.
Xcode project is attached.
Thanks!