Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

More CPtrArray sorting problems!

Posted on 2008-06-18
5
816 Views
Last Modified: 2013-11-20
Ah hello.

A follow up to a previous question about sorting CPtrArrays.  Consider this code:


class CMyClass
{
public:
      CMyClass ( DWORD d ) : m_dw ( d ) {}
      ~CMyClass() {}
      DWORD m_dw;
      CString s;
};

typedef int (__cdecl *GENERICCOMPAREFN)(const void * elem1, const void * elem2);
typedef int (__cdecl *STRINGCOMPAREFN)(const CMyClass * elem1, const CMyClass * elem2);

class CMyArray : public CPtrArray
{
public:
      void Sort(GENERICCOMPAREFN pfnCompare = Compare)
      {
            qsort ( GetData(), GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );
      }
protected:

      static int __cdecl Compare(const void * pMyClass1, const void * pMyClass2 )
      {
            if ( !pMyClass1 || !pMyClass2 ) return 0;
            CMyClass* pActualMyClass1 = *(CMyClass**)pMyClass1;
            CMyClass* pActualMyClass2 = *(CMyClass**)pMyClass2;

            return pActualMyClass1 ->m_dw < pActualMyClass2 ->m_dw;
      }
};

int main()
{
      CMyArray m_array;

      m_array.Add ( ( void*) new CMyClass ( 1 ) );
      m_array.Add ( ( void*) new CMyClass ( 3 ) );
      m_array.Add ( ( void*) new CMyClass ( 2 ) );

            m_array.Sort();

            return 0;
}

I am finding that whenever I have a CString member in the class object contained in my array, the code generates an access violation in the Compare() functoin.  More specifically, the first time the function is called by qsort(), I get my two pointers pActualMyClass1 and pActualMyClass2 both valid.  The second time however, pMyClass1 is always 0xcdcdcdcd.  So the attempt to access m_dw fails.

If I remove the CString member, this does not happen.

Can someone please tell me What is going on here, and how to get around it?  I need my CString member.

TIA
0
Comment
Question by:mrwad99
  • 2
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 125 total points
ID: 21813646
Hi mrwad99,

I guess this line is wrong:

>             qsort ( GetData(), GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );

instead of 'sizeof ( CMyClass )' you should pass 'sizeof ( CMyClass* )' since you compare pointers to CMyClass, not instances of CMyClass.

Hope that helps,

ZOPPO
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21813883
Thank you ZOPPO :o)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 21813907
You're welcome ...
0
 
LVL 19

Expert Comment

by:alb66
ID: 21813914
Zoppo is right. The previous case(without CString) worked only because
sizeof(DWORD) == sizeof(void*)
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21813932
Ah, I see!  Thanks for the follow up alb66.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ question 3 70
Excel file not created as expected 7 79
Authenticate using sesu from script 7 54
Increment column based of a FK 8 31
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question