One thing to watch out for is the type of the variables. We will use the same sorting procedure from the previous lecture as an example.
In line,
for (us = 1; us <= nelt; us++) {
"us" is a signed int whereas "nelt" is an unsigned integer.
Could there be any problems ? The answer is YES. Let's see,
On a 32-bit machine the range of signed integer is from (-2^31) to (2^31 - 1) whereas the range of unsigned ineger is from 0 to (2^32 -1)
Lets assume, nelt = 2^31
us++ is going to overflow after (2^31 -1) and we will get to
the largest negative number (-2^31) and the inequality
us < nelt
will still hold.
Some compilers generate warning, some implementations convert the int
type to unsigned int type.
You can write a short program to experiment such kind of things:-
main ()
{
unsigned int x;
int y;
x = 0x80000000;
y = 0x80000000;
if ( y < x)
printf ("yes");
// no type cast.
else
printf("no");
// int has been converted to unsigned int.
}
Please Note:
It is important how you interpret the bits. This is going to help you
in Assignment 1 too.Take the instruction, figure out what the bit
pattern is, group them into 8 bits, and note the ASCII character corresponding
to this pattern. Some useful "gdb" commands for doing
this:-
x/c
x/s
x/i
For a complicated program you have to generate a whole lot of test cases,
like the one that stresses the program, but always make sure to
be aware of what the O/P is going to be.
sort(int *array, int nelt)
{
int i;
for( i=0; i < nelt; i++)
array[i] = i;
}
More to follow in the upcoming lectures.