ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
when you are doing a
you are just subtracting the pointers. are you sure this is what you want to do? do you want to sort the list according the address of the owner or is it according to some content?
This is just a guess, but say the second pointer is larger than the first: 0x12345678 - 0xFF000000
After the operation, the result is supposed to be negative, but what exactly is a negative pointer? There's no such thing as a negative memory address, and I have no clue how the compiler would interpret it. Would it scale the pointer operation? Say sizeof(struct FullTableInfo) equals 16 bytes. Would the subtraction be equivalent to 0x12345678 - ( 0xFF000000 * 16)?
Even though the return result is declared as an int, I doubt the pointer values are altered to ints during the evaluation. What I'm getting at is say you have "float = int * float". The int is promoted to float before the expression is evaluated. Here, you have "int = pointer - pointer". I don't know that the compiler would do any data type conversion until after the subtraction was finished.
Anyway, if you're in the experimentation mood, I would suggest trying this code:
You are right there is no such a thing as negative memory address but just returning
a negative number or positive number will tell qsort() to do the sorting assending or descending
and that's what qsort needs to know. I tried to return 1, -1, 0 but that still gave me the wrong
Well, what I'm getting at is this: if there is no data type upgrade when the expression is evaluated, the compiler needs to make a decision on what to do. It has a negative value that it's supposed to interpret as a pointer. It has to interpret it as a pointer before the value can be cast to an int. So what does it do with it? Take the absolute value? "Wrap" the memory space?
The sortIt() function makes the assumption that you can use pointers as ordinary ints. I'm saying that assumption might be incorrect. If pointers behaved the same way as ints (or more appropriately unsigned ints), then why would there be separate data types?
When you subtract two pointers, it will return the difference divided by the size of the dereferenced pointer. For example:
&a - &b
is equivalent to (on a 32-bit machine)
(( int )&a - ( int )&b) / sizeof( int )
You can easily test this with arrays:
int d1 = &a - &a;
int d2 = &a - &a;
printf("%d, %d\n", d1, d2 );
This will print 2, -3 (regardless whether it is int a, char a or any other type).
That makes sense... It tells how many objects are between the two addresses (assuming a contiguous array). Given that, I can't think of a reason why the sortIt() function failed when using pointer subtraction, but worked as expected when typecasting the pointers to ints.
I'm at a loss...
Last edited by Dark_Helmet; 09-27-2005 at 02:14 AM.