Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple iterator problem

Reply
Thread Tools

Simple iterator problem

 
 
saneman
Guest
Posts: n/a
 
      08-02-2008
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 200 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?


 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      08-02-2008

"saneman" <(E-Mail Removed)> wrote in message
news:489486d6$0$90272$(E-Mail Removed)...
>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 200 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++;
}
}




 
Reply With Quote
 
 
 
 
Rajib
Guest
Posts: n/a
 
      08-03-2008
Daniel T. wrote:
> "saneman" <(E-Mail Removed)> wrote:
>
>> 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 200 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?

>
> What is the code that comes after this block?


It could also be code before that block that is causing the problem.
 
Reply With Quote
 
Rajib
Guest
Posts: n/a
 
      08-03-2008
Daniel T. wrote:
> Rajib <(E-Mail Removed)> wrote:
>> Daniel T. wrote:
>>> "saneman" <(E-Mail Removed)> wrote:
>>>
>>>> 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 200 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?
>>> What is the code that comes after this block?

>> It could also be code before that block that is causing the problem.

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments