Thread: Crash when sorting from an std::vector Help?

  1. #1
    alkis_y3k
    Guest

    Crash when sorting from an std::vector Help?

    I use qsort to sort an std::vector's items, based on a value they have.

    This is my code:
    Code:
    typedef int (*compfn)(const void*, const void*);
    
    inline int comp(vector<CRTStaticMesh*>::const_iterator& a, vector<CRTStaticMesh*>::const_iterator& b)
    {
    	return (static_cast<CRTStaticMesh*>(*a)->GetPosition(NULL)->z - static_cast<CRTStaticMesh*>(*b)->GetPosition(NULL)->z);
    }
    
    qsort((void*)&lst.front(), lst.size(), sizeof(float), (compfn)(comp));
    It crashes on the qsort line. Anyone knows why?

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>It crashes on the qsort line. Anyone knows why?
    My guess would be that qsort isn't capable of understanding how a standard template container is set up. Why not just use sort() from <algorithm> instead?
    Code:
    sort(lst.begin(), lst.end(), comp);
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM