ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am using qsort() to sort an array of structure (not array of pointers), about 145 structures but I it does not sort correctly. This is used in a
C++ code
This code compile and produce the right results in Solaris 2.6 & windows2000 but not in linux redhat 9
here is part of my code:
struct FullTableInfo
{
int a;
char s;
CElement *owner; // owner is a pointer to a class object CElement.
};
main()
{
struct FullTableInfo *FullTable = new struct FullTableInfo[145];
// I fill the array with data and the owner pointer pointing to different
// CElement some of the FullTable[] are pointing to the same CElement
// the results does not look that it is sorted right?
// if we are sorting owner1, owner2, owner3.....etc
// i get ownere 1 and then owner2 and then back to owner1
any ideas. could it be a bug in qsort() or the g++ compiler i am using??
Thanks
}
static int sortIt (const void *thisone, const void *theotherone)
{
// I am sorting of the address of owner
when you are doing a
theOne->owner-theOther->owner
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?
Thanks for you reply. I need to sort it by the address of the owner so all the owners
with the same address should be grouped together. Some how when I casted the address
to (int) the sorting works??
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
sorting.
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?
So what do you think I should do? I tried your suggestion by returning 1, -1, 0
and that did not work. Do you think if I cast the pointer to their original data types
that will work ?
All I'm suggesting is to avoid using a pointer in mathematical operations. If you must use them that way, then typecast them, or assign them to temporary variables of the appropriate type:
Code:
static int sortIt (const void *thisone, const void *theotherone)
{
// I am sorting of the address of owner
int theOne_owner_address;
int theOther_owner_address;
struct FullTableStruct *theOne = (struct FullTableStruct *)thisone;
struct FullTableStruct *theOther = (struct FullTableStruct *)theotherone;
theOne_owner_address = (int)theOne->owner;
theOther_owner_address = (int)theOther->owner;
return( theOne_owner_address - theOther_owner_address);
}
EDIT: removed bad example... not comparing apples to apples...
Last edited by Dark_Helmet; 09-26-2005 at 01:59 PM.
When you subtract two pointers, it will return the difference divided by the size of the dereferenced pointer. For example:
int a;
int b;
&a - &b
is equivalent to (on a 32-bit machine)
(( int )&a - ( int )&b) / sizeof( int )
You can easily test this with arrays:
int a[10];
int d1 = &a[5] - &a[3];
int d2 = &a[5] - &a[8];
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.
I am using g++ compiler with -g for the debug version and -O3 for the optimize version.
Also I am using the -Wno-deprecated
One thing is worth to mention that bsearch() (binary search) did not work for me either
in linux (red hat 9) but again, the same source code did work on Solaris, Windows, HP
As you can see I port the same source code on different platforms.
I started to believe it could be a compiler issue (flags needs to be set) since I don't
think qsort() & bsearch() will fail and they are popular functions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.