Vector Assertion Failure.
-
Hi everyone, This is my code below, the first time I run through this code it's fine but when it hits the for loop a second time my Iter = -768 (or something like that) and it gives me an assertion error. The first time it runs through this loop it always uses the else statement (if that's a clue) not the if. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { size++; if (size < PrimeNumber.size() -1) *(Iter + 1) += 1; else PrimeNumber.push_back(1); } Does anyone have any ideas? Thanks for the attention and help in advance, I appreciate it. Cheers, Michael :)
-
Hi everyone, This is my code below, the first time I run through this code it's fine but when it hits the for loop a second time my Iter = -768 (or something like that) and it gives me an assertion error. The first time it runs through this loop it always uses the else statement (if that's a clue) not the if. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { size++; if (size < PrimeNumber.size() -1) *(Iter + 1) += 1; else PrimeNumber.push_back(1); } Does anyone have any ideas? Thanks for the attention and help in advance, I appreciate it. Cheers, Michael :)
Michael101 wrote:
size++;
Is this initialised to zero somewhere? This line should be at the end of the loop. The first time through size is probably one and Im guessing PrimeNumber.size() is one?. Therefore the condition is if(1 < 0) which would both be false. What exatcly do you want this code to do?
-
Michael101 wrote:
size++;
Is this initialised to zero somewhere? This line should be at the end of the loop. The first time through size is probably one and Im guessing PrimeNumber.size() is one?. Therefore the condition is if(1 < 0) which would both be false. What exatcly do you want this code to do?
My code is calculating numbers together in the vector and based on certain conditions it must build another element in the vector to keep the equation going. If not, eventually the number will be too big for longs, int, short e.t.c.... When it creates a new element it always returns to that for loop code and it the iterator is always equal to -274 which is just wrong. It should be one more to what it left off as. The code is posted below, it's a complex equation but it might help. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { //*Iter = PrimeNumber[y]; *(Iter) += *Iter; if (*Iter > 9) { PrimeNumber[0] -= 10; for (Iter1 = PrimeNumber.begin(); Iter1 != PrimeNumber.end(); Iter1++) { Counter++; if (Counter = PrimeNumber.size() - 1) *(Iter + 1) += (*(Iter + 1)) + 1; else PrimeNumber.push_back(1); if (PrimeNumber[Counter] > 9) { if (Counter < PrimeNumber.size()) { PrimeNumber[Counter] -= 10; PrimeNumber[Counter + 1] += 1; } else PrimeNumber.push_back(1); } break; } Counter = 0; } Ultimately, this code increaments numbers into the vector and if the number is 16 in the vector is displays as [6][1] (Computers write backwards of course). It's when it makes the new element for the 1 it goes nuts on the Iterator. Thanks for your interest, I appreciate the help! Michael :)
-
My code is calculating numbers together in the vector and based on certain conditions it must build another element in the vector to keep the equation going. If not, eventually the number will be too big for longs, int, short e.t.c.... When it creates a new element it always returns to that for loop code and it the iterator is always equal to -274 which is just wrong. It should be one more to what it left off as. The code is posted below, it's a complex equation but it might help. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { //*Iter = PrimeNumber[y]; *(Iter) += *Iter; if (*Iter > 9) { PrimeNumber[0] -= 10; for (Iter1 = PrimeNumber.begin(); Iter1 != PrimeNumber.end(); Iter1++) { Counter++; if (Counter = PrimeNumber.size() - 1) *(Iter + 1) += (*(Iter + 1)) + 1; else PrimeNumber.push_back(1); if (PrimeNumber[Counter] > 9) { if (Counter < PrimeNumber.size()) { PrimeNumber[Counter] -= 10; PrimeNumber[Counter + 1] += 1; } else PrimeNumber.push_back(1); } break; } Counter = 0; } Ultimately, this code increaments numbers into the vector and if the number is 16 in the vector is displays as [6][1] (Computers write backwards of course). It's when it makes the new element for the 1 it goes nuts on the Iterator. Thanks for your interest, I appreciate the help! Michael :)
What you're actually trying to achieve is beyond me but
Michael101 wrote:
Counter++;
Where is this initialised? It appears to me it should be set to 0 before the second for loop and incremented at the end of the second for loop not at the start. When you say the iter is equal to -274 is that the actual value of iter or (*iter)? Ie is it the value of the iterator or the value of the underlying variable at that position? When writing loops with stl containers I often take a reference to the value behind the iter and use that in the loop as it makes the code clearer. Here is an example assuming your vector contains ints for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { int& currentValue = (*iter); // now use currentValue rather than (*iter) }
-
What you're actually trying to achieve is beyond me but
Michael101 wrote:
Counter++;
Where is this initialised? It appears to me it should be set to 0 before the second for loop and incremented at the end of the second for loop not at the start. When you say the iter is equal to -274 is that the actual value of iter or (*iter)? Ie is it the value of the iterator or the value of the underlying variable at that position? When writing loops with stl containers I often take a reference to the value behind the iter and use that in the loop as it makes the code clearer. Here is an example assuming your vector contains ints for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { int& currentValue = (*iter); // now use currentValue rather than (*iter) }
Iter is initialised at the beginning of the program like this -> register vector::iterator Iter; The -274 is the actual value of the Iterator but now that I've debugged it further I don't think that matters because on other occasions it's equal to something silly before the loop runs through again but the program doesn't fall over. Have you ever had problems with Iterators after adding in a container to your vector? I will try your sample code above because I understand it clearly and does seem to be much safer. Since this algorithm runs for long periods of time speed is important but for now I'll ignore that. Cheers for the reply, thanks for the help, Michael :)
-
Iter is initialised at the beginning of the program like this -> register vector::iterator Iter; The -274 is the actual value of the Iterator but now that I've debugged it further I don't think that matters because on other occasions it's equal to something silly before the loop runs through again but the program doesn't fall over. Have you ever had problems with Iterators after adding in a container to your vector? I will try your sample code above because I understand it clearly and does seem to be much safer. Since this algorithm runs for long periods of time speed is important but for now I'll ignore that. Cheers for the reply, thanks for the help, Michael :)
Ahh I found the problem... The Iterator equals the actual element in the Vector not the element number! Well I think that's the error
-
Iter is initialised at the beginning of the program like this -> register vector::iterator Iter; The -274 is the actual value of the Iterator but now that I've debugged it further I don't think that matters because on other occasions it's equal to something silly before the loop runs through again but the program doesn't fall over. Have you ever had problems with Iterators after adding in a container to your vector? I will try your sample code above because I understand it clearly and does seem to be much safer. Since this algorithm runs for long periods of time speed is important but for now I'll ignore that. Cheers for the reply, thanks for the help, Michael :)
Michael101 wrote:
Iter is initialised at the beginning of the program like this -> register vector::iterator Iter;
Why are you using register? I would definatly get rid of that at least for the time being. I was asking about the initialisation of Counter. Where is it initialised?
Michael101 wrote:
Have you ever had problems with Iterators after adding in a container to your vector?
I'm pretty sure adding a value to the end of your vector shuold not invalidate your iterators but you might want to see if you can verify that. Google should help. If you supect thats an issue add new values to a temp vector and then copy them to your real vector after you've finished iterating throught it. The STL is birilliant but its not an easy beast to master. I sugest a couple of good books like Effective STL by Meyers
-
Michael101 wrote:
Iter is initialised at the beginning of the program like this -> register vector::iterator Iter;
Why are you using register? I would definatly get rid of that at least for the time being. I was asking about the initialisation of Counter. Where is it initialised?
Michael101 wrote:
Have you ever had problems with Iterators after adding in a container to your vector?
I'm pretty sure adding a value to the end of your vector shuold not invalidate your iterators but you might want to see if you can verify that. Google should help. If you supect thats an issue add new values to a temp vector and then copy them to your real vector after you've finished iterating throught it. The STL is birilliant but its not an easy beast to master. I sugest a couple of good books like Effective STL by Meyers
Effective STL by Meyers, yeah ok I'll buy it! Thanks for all your help, I have to leave this error for later but I'm sure I'll crack it soon. It's just finiky that's all. Thanks for all your help, I appreciate it! Michael :)
-
Hi everyone, This is my code below, the first time I run through this code it's fine but when it hits the for loop a second time my Iter = -768 (or something like that) and it gives me an assertion error. The first time it runs through this loop it always uses the else statement (if that's a clue) not the if. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { size++; if (size < PrimeNumber.size() -1) *(Iter + 1) += 1; else PrimeNumber.push_back(1); } Does anyone have any ideas? Thanks for the attention and help in advance, I appreciate it. Cheers, Michael :)
I have read your talk with Josh Gray, and I have only a few points to add. a) Adding to a
vector
*will* invalidate alliterator
in it. Not every time, but sometimes, and you cant control it. A vector is guaranteed to be in a continuous part of memory, and when it gets bigger, it is reallocated. All Iterators are invalidated. There are things you might try (reserve()
), but it will not work most of the time. b) The register you put before the iterator-declaration is silly. Aniterator
is a class and will certainly not fit into a register. c) in thefor
-loop, increment theiterator
using the postfix-increment-operator:++Iter
. Also,try to dereference theiterator
as few as possible. You are getting a large overhead. Your program will be much faster in release, and stop being unusable slow in debug mode (All with VS.NET 2005).
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
Hi everyone, This is my code below, the first time I run through this code it's fine but when it hits the for loop a second time my Iter = -768 (or something like that) and it gives me an assertion error. The first time it runs through this loop it always uses the else statement (if that's a clue) not the if. for (Iter = PrimeNumber.begin(); Iter != PrimeNumber.end(); Iter++) { size++; if (size < PrimeNumber.size() -1) *(Iter + 1) += 1; else PrimeNumber.push_back(1); } Does anyone have any ideas? Thanks for the attention and help in advance, I appreciate it. Cheers, Michael :)
Michael101 wrote:
...it gives me an assertion error.
What line of what file is asserting?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne