Simple iterator problem

S

saneman

I have made the following code:

std::vector<int> v;
std::vector<int>::iterator it;
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(1);
v.push_back(1);
it = v.begin();
while (it != v.end()) {
std::cout << *it << std::endl;
it++;
}

It compiles fine (using MS VS 2008) and when I run it it also prints 0 0 0 1
1 1. But then I get:

Debug Error!

Invalid allocation size 492.....bytes


and I need to pres either abort, try again or ignore. Is this a windows
specific issue?
 
K

kwikius

saneman said:
I have made the following code:

std::vector<int> v;
std::vector<int>::iterator it;
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(1);
v.push_back(1);
it = v.begin();
while (it != v.end()) {
std::cout << *it << std::endl;
it++;
}

It compiles fine (using MS VS 2008) and when I run it it also prints 0 0 0
1 1 1. But then I get:

Debug Error!

Invalid allocation size 492.....bytes

Well I cant see anything wrong with the code. Try running it on its own as
code below

#include <vector>
#include <iostream>

int main()
{
std::vector<int> v;
std::vector<int>::iterator it;
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(1);
v.push_back(1);
it = v.begin();
while (it != v.end()) {
std::cout << *it << std::endl;
it++;
}
}
 
R

Rajib

Daniel said:
:) It could be code half-way across the program! But we have to start
somewhere, don't we. The point is, the OP didn't post the code that has
the problem...

Yup, I'm just making sure the OP didn't have the idea that because the
debug error showed up after the output the mistake must have also
occurred after that piece of code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top