Click here to Skip to main content
15,908,768 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: #pragma pack(1) packing wrong structure sizes ! ! ! Pin
Alan Chambers28-Jan-05 11:05
Alan Chambers28-Jan-05 11:05 
QuestionHow to use shared-memory to map uncertain page file Pin
shusong21-Jan-05 14:06
shusong21-Jan-05 14:06 
AnswerRe: How to use shared-memory to map uncertain page file Pin
Bob Ciora21-Jan-05 19:47
Bob Ciora21-Jan-05 19:47 
QuestionHow to draw a ListView frame Pin
Björn Eiríksson21-Jan-05 13:25
sussBjörn Eiríksson21-Jan-05 13:25 
QuestionHow to perform qsort on a CList of class objects? Pin
wsquare21-Jan-05 12:51
wsquare21-Jan-05 12:51 
AnswerRe: How to perform qsort on a CList of class objects? Pin
wsquare21-Jan-05 12:56
wsquare21-Jan-05 12:56 
AnswerRe: How to perform qsort on a CList of class objects? Pin
Anonymous21-Jan-05 13:31
Anonymous21-Jan-05 13:31 
AnswerRe: How to perform qsort on a CList of class objects? Pin
Bob Ciora21-Jan-05 18:48
Bob Ciora21-Jan-05 18:48 
The problem is that CList (or any linked list for that matter) doesn't lend itself well to the C runtime library's qsort() routine. qsort basically sorts an array in place, swapping entire array entries if necessary. So you first have to guarantee that all items to be sorted are in one contiguous memory block. The CList class doesn't make this guarantee.

Secondly, qsort doesn't care about the links to neighboring items. It'll just move them around blindly as it moves the object (and doesn't notify you when it does so). If qsort() moves an item referenced by another item's neighbor link, then that link is no longer valid, since it doesn't point to the original neighbor anymore. qsort doesn't care about maintaining these links...it just swaps objects blindly.

So qsort() is basically a crappy choice for linked lists.

If you're determined to use qsort(), then you'll most likely have to abandon CList as your container type in favor of something that provides a reliable array that qsort can operate on. This means that you must be sure that all elements of the array occupy one contiguous memory block. I don't think even CArray guarantees that (if you're also determined to use an MFC collection type).

So here are some things to think about if you *must* use qsort:

- If you know the maximum size the list will reach, then you can simply create a single array of that size and populate it as your program runs. You'll have to make sure to clean up "holes" when items are removed (by sliding later elements backward to fill the vacancy). This will guarantee that all sortable items will be in one contiguous block. New items get added to the end, and the list is sorted as new items are added.

- If you don't know the list's maximum size, then you'll need to develop your own resizable array class that guarantees that all items are in one contiguous memory block. The array should be able to grow (and shrink) when needed, but must always guarantee congiguous memory for qsort.


If qsort() isn't a requirement, though, then you can consider some other means of sorting the list. Linked lists, though, don't sort too well, since you can really only sort them sequentially.

However, if you're application is using num1 and num2 (and maybe x) to reference some unique CString (modelName), then you may want to consider using the CMap class instead. CMap is basically a hash table, which lends itself well to this sort of application (mapping some key value or values to a unique item). If you can provide a means to provide a unique "key" value by combining num1 and num2, you can use that key value as an index into the CMap to select the string (or entire matchData object).

For example, if you know that num1 and num2 will never exceed 10,000, then you can define your "key" value for the CMap as follows:
<br />
double CalculateKey(double num1, double num2) <br />
{ <br />
  return( num1 * 10000 + num2 );<br />
}<br />


Define your CMap as follows:
<br />
typedef CMap<double, double, matchData *, const matchData *> matchList;<br />
matchList Result;<br />


Then adding a new matchData item to the list is simple:
<br />
matchData *pdataToAdd;<br />
...<br />
...<br />
double key = CalculateKey(pdataToAdd->GetNum1(), pdataToAdd->GetNum2());<br />
Result[key] = pDataToAdd;<br />


Locating an item is also simple:
<br />
double num1ToFind, num2ToFind;<br />
matchData * pfoundData;<br />
...<br />
...<br />
double key = CalculateKey(num1ToFind, num2ToFind);<br />
BOOL bFound = Result.Lookup(key, pfoundData);<br />
if( bFound )<br />
{<br />
  // found a match!  pfoundData holds the (matchData *) from the Map<br />
}<br />
else<br />
{<br />
  // No match found, pfoundData is undefined...<br />
}<br />


Note that you can't use the [] operator when searching for an item. Check out the documentation for CMap over at the MSDN site. Here's the documentation for CMap

I sure hope all this helped, and good luck!!

Bob Ciora
GeneralRe: How to perform qsort on a CList of class objects? Pin
wsquare21-Jan-05 19:58
wsquare21-Jan-05 19:58 
GeneralRe: How to perform qsort on a CList of class objects? Pin
markkuk22-Jan-05 0:03
markkuk22-Jan-05 0:03 
GeneralRe: How to perform qsort on a CList of class objects? Pin
Bob Ciora22-Jan-05 3:25
Bob Ciora22-Jan-05 3:25 
GeneralRe: How to perform qsort on a CList of class objects? Pin
markkuk23-Jan-05 4:31
markkuk23-Jan-05 4:31 
GeneralRe: How to perform qsort on a CList of class objects? Pin
Bob Ciora22-Jan-05 4:22
Bob Ciora22-Jan-05 4:22 
QuestionHow do I assign char array address to int? Pin
ErikDabrowsky21-Jan-05 10:26
ErikDabrowsky21-Jan-05 10:26 
AnswerRe: How do I assign char array address to int? Pin
Michael Dunn21-Jan-05 11:00
sitebuilderMichael Dunn21-Jan-05 11:00 
GeneralRe: How do I assign char array address to int? Pin
ErikDabrowsky21-Jan-05 11:29
ErikDabrowsky21-Jan-05 11:29 
AnswerRe: How do I assign char array address to int? Pin
markkuk22-Jan-05 0:19
markkuk22-Jan-05 0:19 
GeneralMFC app. + Ms-Dos commands Pin
holy shit21-Jan-05 9:12
holy shit21-Jan-05 9:12 
GeneralRe: MFC app. + Ms-Dos commands Pin
Chris Losinger21-Jan-05 9:19
professionalChris Losinger21-Jan-05 9:19 
GeneralRe: MFC app. + Ms-Dos commands Pin
holy shit21-Jan-05 9:28
holy shit21-Jan-05 9:28 
GeneralRe: MFC app. + Ms-Dos commands Pin
Shog921-Jan-05 9:32
sitebuilderShog921-Jan-05 9:32 
GeneralRe: MFC app. + Ms-Dos commands Pin
Chris Losinger21-Jan-05 9:34
professionalChris Losinger21-Jan-05 9:34 
GeneralRe: MFC app. + Ms-Dos commands Pin
Michael Dunn21-Jan-05 11:01
sitebuilderMichael Dunn21-Jan-05 11:01 
GeneralRe: MFC app. + Ms-Dos commands Pin
Shog921-Jan-05 11:21
sitebuilderShog921-Jan-05 11:21 
GeneralRe: MFC app. + Ms-Dos commands Pin
Michael Dunn21-Jan-05 12:05
sitebuilderMichael Dunn21-Jan-05 12:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.