Simple iterator problem

Discussion in 'C++' started by saneman, Aug 2, 2008.

  1. saneman

    saneman Guest

    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?
    saneman, Aug 2, 2008
    #1
    1. Advertising

  2. saneman

    kwikius Guest

    "saneman" <> wrote in message
    news:489486d6$0$90272$...
    >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++;
    }
    }
    kwikius, Aug 2, 2008
    #2
    1. Advertising

  3. saneman

    Rajib Guest

    Daniel T. wrote:
    > "saneman" <> 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 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?

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


    It could also be code before that block that is causing the problem.
    Rajib, Aug 3, 2008
    #3
  4. saneman

    Rajib Guest

    Daniel T. wrote:
    > Rajib <> wrote:
    >> Daniel T. wrote:
    >>> "saneman" <> 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 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?
    >>> 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.
    Rajib, Aug 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,398
  2. greg
    Replies:
    6
    Views:
    443
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    625
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,128
    Steven D'Aprano
    Apr 20, 2007
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    108
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page