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. Issues with std::vector

Issues with std::vector

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsdebuggingperformancehelp
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.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    Is there some limitation on the vector template that I should know about? I have a vector of 45,000 string pointers. It's a dictionary file ;). Anyway, I need to iterate through these strings checking if more than one definition is present on the same line. Multiple definition are seperated with the '|' character, when I find one I create a new string and push it to the end of the vector. Simple enough you would think. So, here is my code.

    vector< LPENTRY >::iterator vIter = vEntries.begin();

    while ( vIter != vEntries.end() )
    {
        LPSTR szEng = strchr( (\*vIter)->English, '|' );
    
        if ( szEng != NULL )
        {
            // Create a new entry
            LPENTRY pEntry = new ENTRY;
    
            // Copy the contents ( the struct arranges new memory and copies the strings )
            \*pEntry = \*vIter;
    
            // Copy the new English
            strcpy\_s( pEntry->English, MAX\_LINE\_LENGTH, &szEng\[ 1 \] );
    
            // Shorten the old string
            \*szEng = 0x00;
    
            vEntries.push\_back( pEntry );
        }
    
        vIter++;
    }
    

    The problem is, when the vector grows to a size of 61447 and when pushing a new value to the end, it causes the iterator to become invalid ( pointing to 0xfeeefeee ). Hence when trying to increase it it triggers a breakpoint. Any ideas what is going wrong?

    Waldermort

    S 1 Reply Last reply
    0
    • W Waldermort

      Is there some limitation on the vector template that I should know about? I have a vector of 45,000 string pointers. It's a dictionary file ;). Anyway, I need to iterate through these strings checking if more than one definition is present on the same line. Multiple definition are seperated with the '|' character, when I find one I create a new string and push it to the end of the vector. Simple enough you would think. So, here is my code.

      vector< LPENTRY >::iterator vIter = vEntries.begin();

      while ( vIter != vEntries.end() )
      {
          LPSTR szEng = strchr( (\*vIter)->English, '|' );
      
          if ( szEng != NULL )
          {
              // Create a new entry
              LPENTRY pEntry = new ENTRY;
      
              // Copy the contents ( the struct arranges new memory and copies the strings )
              \*pEntry = \*vIter;
      
              // Copy the new English
              strcpy\_s( pEntry->English, MAX\_LINE\_LENGTH, &szEng\[ 1 \] );
      
              // Shorten the old string
              \*szEng = 0x00;
      
              vEntries.push\_back( pEntry );
          }
      
          vIter++;
      }
      

      The problem is, when the vector grows to a size of 61447 and when pushing a new value to the end, it causes the iterator to become invalid ( pointing to 0xfeeefeee ). Hence when trying to increase it it triggers a breakpoint. Any ideas what is going wrong?

      Waldermort

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Firstly, see here[^] for the meaning of 0xfeeefeee. See here[^] for a description of "iterator invalidation". Here's a quote:

      [2] Memory will be reallocated automatically if more than capacity() - size() elements are inserted into the vector. Reallocation does not change size(), nor does it change the values of any elements of the vector. It does, however, increase capacity(), and it invalidates [5] any iterators that point into the vector.

      Steve

      W 1 Reply Last reply
      0
      • S Stephen Hewitt

        Firstly, see here[^] for the meaning of 0xfeeefeee. See here[^] for a description of "iterator invalidation". Here's a quote:

        [2] Memory will be reallocated automatically if more than capacity() - size() elements are inserted into the vector. Reallocation does not change size(), nor does it change the values of any elements of the vector. It does, however, increase capacity(), and it invalidates [5] any iterators that point into the vector.

        Steve

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        That value of 0xfeeefeee should have given it away. I worked around the issue by throwing out the iterator and used the indexing method instead. Though thanks for the reply :)

        Waldermort

        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