Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
I am running on Windows 7 Enterprise and I have a routine that gathers data from various sources and formats that data into 30 character entries in a rather large array (just under 104k entries). I use qsort to sort the character data into ascending order using a single key with offset = 0, length = 30 and count = 103964. All very straightforward. The sort seems to make some attempt at ordering the data since I can observe that the initial entries have been put into ascending order but a simple loop comparing array(k) to array(k+1) finds numerous cases where the entries are out of order. Jumping to the end of the array (I'm using the debugger), I can observe that the entries are nowhere near any kind of order. It is as if the sorting algorithm died mid-stream. Is there a way I can determine if qsort is quitting on me? (unfortunately, it does not return any value upon completion).
 
Thanks
Posted 19-Nov-12 8:48am
Comments
nv3 at 19-Nov-12 14:09pm
   
No, there is no way to check on qsort, except for the normal API. But it would be a big surprise when qsort didn't work in that particular case, as it just does a partition sort and that code has been tested for generations. How about pasting your code via "improve question" and let us look at the place where you called qsort and your comparison function.
tom_delorenzo at 19-Nov-12 15:55pm
   
I agree it has worked for generations. That is why I was stunned to see the data in the state it was in. At my company we have used a standardized interface to qsort for nearly as long as qsort has been around. It has never failed. To the best of my abilities, I have checked and re-checked everything I can think of.
nv3 at 19-Nov-12 16:51pm
   
I would take another look at two things:
(1) Are you giving qsort the correct length per entry? The behaviour your are describing could easily be explained by giving qsort a too short entry length, so that it never tries to even touch the entries at the very end. Perhaps a char / wchar_t error?
(2) Is your compare function correct? Try to give it a couple of cases that you construct by test code and observe in the debugger if things run as they should.
 
I am almost tempted to bet that one of these questions will lead you to the real problem :-) Good luck anyhow; and I am certain that you will find the cause really soon.
tom_delorenzo at 26-Nov-12 13:17pm
   
What I was really wondering was if anyone else had problems with qsort on W7. Unfortunately, I'm up against a hard deadline so, with little time left, I had to write my own sort routine (20X slower but at least my data got sorted correctly). (posted below) User "essn" has a case where qsort works under XP but fails under W7. So, who knows?
 
Anyway, thanks to all who posted advice.
nv3 at 26-Nov-12 14:14pm
   
Tom, I cannot see a scenario in which W7 should have anything to do with it. qsort does not call any operating system functions and is a simple piece of code in the C runtime library. I would be very surprised if that had something to do with W7.
essn at 29-Nov-12 13:38pm
   
Hate to admit it, but qsort not working was my error. qsort works fine for windows 7
nv3 at 29-Nov-12 13:48pm
   
It happens to all of us, once in a while ...
tom_delorenzo at 29-Nov-12 18:43pm
   
OK, I finally got time to look into this further and (sheepishly) here is what I found. Previously,I mentioned that my company uses an interface to qsort that is nearly as old as qsort. Well back then most everyone here wrote in Fortran and, as you are probably aware, Fortran favors indexes rather than offsets and the interface accomodated the Fortran mindset (arrays go from 1 to N not 0 to N-1). So even though the comments described the key as an offset, a length, a sort type and a sort order what it really meant was an index, a length, a sort type and a sort order. Naturally, writing in C++, I was 1 byte off. Hence, the sort "failed". Alas, once again ===> user error.
nv3 at 30-Nov-12 2:22am
   
That is good news, and it shows your courage to admit that it was eventually your fault. Don't worry, once in a while it happens to all of us, even the best. It must feel good to have clarified the issue and my congrats to resolving it. Someone had voted your question down, unfairly in my opinion, and I have tried to counter balance a bit here :-)
CPallini at 19-Nov-12 17:02pm
   
I would say: "it never failed, until now!"
Richard MacCutchan at 19-Nov-12 14:11pm
   
Are you sure that all your parameters are correct, and that your compare() routine is not failing anywhere?
CPallini at 19-Nov-12 14:16pm
   
Code Watson we need code.
qsort cannot fail or, on the opposite, you made a discovery.
essn at 21-Nov-12 13:21pm
   
I am having the same problem. Qsort code works fine on windows xp, but does not sort on Windows 7. Using qsort(pStr, GetSize(), sizeof(CString), (GENERICCOMPAREFN) pFnCompare); Compare is pStr1->Compare(*pStr2). Usning CStrings as noted in the following MS article http://support.microsoft.com/kb/216858

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No way is this a W7 issue. It's a 'your code doesn't work and you should look at it in the debugger first, then post it here if you expect help' issue
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 186
4 Aajmot Sk 153
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100