Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. std::sort() passes invalid parameter to sorting callback function

std::sort() passes invalid parameter to sorting callback function

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsalgorithmsdata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s_k
    wrote on last edited by
    #1

    Hi, I use vector of pointers to my class to store data. I insert them using push_back(). Here is my function for inserting data into vector: [code] UINT CVirtualLWItemManager::insertItem(LS_item* pNewItem) { // check if current capacity is enough if (m_vecItems.size() > m_vecItems.capacity()) { AfxMessageBox("m_vecItems.size() > m_vecItems.capacity()"); return 0; } if (m_vecItems.size() == m_vecItems.capacity()) { // enlarge array m_vecItems.reserve(m_vecItems.capacity() + 128); // try again return insertItem(pNewItem); } m_vecItems.push_back(pNewItem); return 1; } [/code] Here is my function that launches std::sort(): [code] void CVirtualLWItemManager::sortItems(INT (*sort_cb)(LS_item*, LS_item*)) { std::sort(m_vecItems.begin(), m_vecItems.end(), sort_cb); } [/code] And here is my static sorting callback function: [code] INT CMyFileView::sortCallback(LS_item* pDataFirst, LS_item* pDataSecond) { ITEM_DESC* param1 = NULL; ITEM_DESC* param2 = NULL; param1 = (ITEM_DESC*) pDataFirst->lParam; param2 = (ITEM_DESC*) pDataSecond->lParam; INT iSortColumn = 0; if (getDoc()->iViewToSort == 1) iSortColumn = getDoc()->SortingColumnLeft; else iSortColumn = getDoc()->SortingColumnRight; // Nejdrive (..), pak adresar, dale soubor if (param1->folder > param2->folder) return -1; else if (param1->folder < param2->folder) return 1; // Trizeni podle zvoleneho sloupce switch (iSortColumn) { // ASCENDING case 0: // Podle nazvu souboru a adresaru return param1->name.CompareNoCase((LPCTSTR) param2->name); case 1: // Podle velikosti if(param1->size < param2->size) return -1; if(param1->size > param2->size) return 1; break; case 2: // Podle data if (param1->date < param2->date) return -1; if (param1->date > param2->date) return 1; break; // DESCENDING case 3: // Podle nazvu souboru a adresaru return (param1->name.CompareNoCase((LPCTSTR) param2->name)) * -1; case 4: // Podle velikosti if (param1->size < param2->size) return 1; if (param1->size > param2->size) return -1; break; case 5: // Podle data if (param1->date < param2->date) return 1; if (param1->date > param2->date) return -1; break; } return 0; } [/code] The problem is, sometimes pDataSecond parameter passed to this function has invalid parameter, eg. 0xfdfdfdfd. How is it possible? Objects that are inserted to the vector are created on the heap.

    J 1 Reply Last reply
    0
    • S s_k

      Hi, I use vector of pointers to my class to store data. I insert them using push_back(). Here is my function for inserting data into vector: [code] UINT CVirtualLWItemManager::insertItem(LS_item* pNewItem) { // check if current capacity is enough if (m_vecItems.size() > m_vecItems.capacity()) { AfxMessageBox("m_vecItems.size() > m_vecItems.capacity()"); return 0; } if (m_vecItems.size() == m_vecItems.capacity()) { // enlarge array m_vecItems.reserve(m_vecItems.capacity() + 128); // try again return insertItem(pNewItem); } m_vecItems.push_back(pNewItem); return 1; } [/code] Here is my function that launches std::sort(): [code] void CVirtualLWItemManager::sortItems(INT (*sort_cb)(LS_item*, LS_item*)) { std::sort(m_vecItems.begin(), m_vecItems.end(), sort_cb); } [/code] And here is my static sorting callback function: [code] INT CMyFileView::sortCallback(LS_item* pDataFirst, LS_item* pDataSecond) { ITEM_DESC* param1 = NULL; ITEM_DESC* param2 = NULL; param1 = (ITEM_DESC*) pDataFirst->lParam; param2 = (ITEM_DESC*) pDataSecond->lParam; INT iSortColumn = 0; if (getDoc()->iViewToSort == 1) iSortColumn = getDoc()->SortingColumnLeft; else iSortColumn = getDoc()->SortingColumnRight; // Nejdrive (..), pak adresar, dale soubor if (param1->folder > param2->folder) return -1; else if (param1->folder < param2->folder) return 1; // Trizeni podle zvoleneho sloupce switch (iSortColumn) { // ASCENDING case 0: // Podle nazvu souboru a adresaru return param1->name.CompareNoCase((LPCTSTR) param2->name); case 1: // Podle velikosti if(param1->size < param2->size) return -1; if(param1->size > param2->size) return 1; break; case 2: // Podle data if (param1->date < param2->date) return -1; if (param1->date > param2->date) return 1; break; // DESCENDING case 3: // Podle nazvu souboru a adresaru return (param1->name.CompareNoCase((LPCTSTR) param2->name)) * -1; case 4: // Podle velikosti if (param1->size < param2->size) return 1; if (param1->size > param2->size) return -1; break; case 5: // Podle data if (param1->date < param2->date) return 1; if (param1->date > param2->date) return -1; break; } return 0; } [/code] The problem is, sometimes pDataSecond parameter passed to this function has invalid parameter, eg. 0xfdfdfdfd. How is it possible? Objects that are inserted to the vector are created on the heap.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      At first sight, there're two problems with your code. The first one is not critical, but anyway: The comparison

      if (m_vecItems.size() > m_vecItems.capacity())

      is always false, so there's no point in checking it. By definition, the size of a vector canot be larger than its capacity. As for

      if (m_vecItems.size() == m_vecItems.capacity())

      it is not really necessary (vectors grow as necessary automatically), but it can improve performance, so no problem about it. The real problem (I guess) is that sort expects a comparison predicate that has less-than semantics, i.e. it expects it to return true is the first argument if less than the second, and false otherwise. Your sortCallback has (<0,0,>0) semantics: change it to what I've indicated and things will probably start to work OK. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      S 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        At first sight, there're two problems with your code. The first one is not critical, but anyway: The comparison

        if (m_vecItems.size() > m_vecItems.capacity())

        is always false, so there's no point in checking it. By definition, the size of a vector canot be larger than its capacity. As for

        if (m_vecItems.size() == m_vecItems.capacity())

        it is not really necessary (vectors grow as necessary automatically), but it can improve performance, so no problem about it. The real problem (I guess) is that sort expects a comparison predicate that has less-than semantics, i.e. it expects it to return true is the first argument if less than the second, and false otherwise. Your sortCallback has (<0,0,>0) semantics: change it to what I've indicated and things will probably start to work OK. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        S Offline
        S Offline
        s_k
        wrote on last edited by
        #3

        It works! I just changed return value of the sorting callback from INT to bool, and replaced 1, -1, 0 with true and false and it's ok now. Thank you Joaquín, you helped me very much!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups