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