iterators - do they allocate memory?

Discussion in 'C++' started by sks_cpp, Jul 3, 2003.

  1. sks_cpp

    sks_cpp Guest

    Consider the following:

    list<int> a; // assume a contains some number of integers

    list<int>::iterator iter = a.find(10);
    for (iter; iter < a.end(); ++iter)
    {
    std::cout << "found one\n";
    }
    ================
    Where are all the memory allocations here?
    There is one for the "iter". Does the find allocate any memory in order to
    create the iterator?
    I suppose the ++iter does?

    Thanks.
    sks_cpp, Jul 3, 2003
    #1
    1. Advertising

  2. sks_cpp

    Ron Natalie Guest

    "sks_cpp" <> wrote in message news:eek:nZMa.369668$...
    > Consider the following:
    >
    > list<int> a; // assume a contains some number of integers
    >
    > list<int>::iterator iter = a.find(10);
    > for (iter; iter < a.end(); ++iter)
    > {
    > std::cout << "found one\n";
    > }
    > ================
    > Where are all the memory allocations here?
    > There is one for the "iter". Does the find allocate any memory in order to
    > create the iterator?
    > I suppose the ++iter does?


    It's really no different than any other type. Objects have to occupy space somewhere.
    Find doesn't allocate space, it just returns a value. There might a temporary generated,
    but your compiler is probably smart enough to just stuff it into iter directly.

    Your program is a bit odd. What do you have against actaully writing your for loops
    like this:

    for(list<int>::iterator iter = a.find(): iter < a.end(); ++iter) {

    If you don't want to use the initializer part of the for, you could just leave it a null statement
    for(; iter < a.end(); ++iter)
    Ron Natalie, Jul 3, 2003
    #2
    1. Advertising

  3. sks_cpp

    Howard Guest

    "sks_cpp" <> wrote in message
    news:eek:nZMa.369668$...
    > Consider the following:
    >
    > list<int> a; // assume a contains some number of integers
    >
    > list<int>::iterator iter = a.find(10);
    > for (iter; iter < a.end(); ++iter)
    > {
    > std::cout << "found one\n";
    > }
    > ================
    > Where are all the memory allocations here?
    > There is one for the "iter". Does the find allocate any memory in order to
    > create the iterator?
    > I suppose the ++iter does?
    >


    Iterators like that above are essentially pointers, aren't they? So the
    only memory allocation would be at the first line, where iter is
    instanciated. Whatever code filled the list out is where space for its
    members was allocated. No additional space is needed.

    Howard
    Howard, Jul 3, 2003
    #3
  4. sks_cpp

    Ron Natalie Guest

    "Howard" <> wrote in message news:be1oe2$...

    > Iterators like that above are essentially pointers, aren't they?


    Not for lists they aren't. Vectors can get away with pointers, but
    how is ++ going to do something intelligent for lists unless it can
    be overloaded, which would mean it has to be a class.
    Ron Natalie, Jul 3, 2003
    #4
  5. sks_cpp

    tom_usenet Guest

    On Thu, 03 Jul 2003 17:05:24 GMT, "sks_cpp" <>
    wrote:

    >Consider the following:
    >
    >list<int> a; // assume a contains some number of integers
    >
    >list<int>::iterator iter = a.find(10);
    >for (iter; iter < a.end(); ++iter)
    >{
    > std::cout << "found one\n";
    >}
    >================
    >Where are all the memory allocations here?


    There are (or should be) no memory allocations from the free store, if
    that's what you're asking.

    >There is one for the "iter". Does the find allocate any memory in order to
    >create the iterator?
    >I suppose the ++iter does?


    Nope. iterators are lightweight objects, typically just containing a
    pointer or two to parts of the container.

    Tom
    tom_usenet, Jul 3, 2003
    #5
  6. sks_cpp

    Howard Guest

    "Ron Natalie" <> wrote in message
    news:3f04667d$0$87909$...
    >
    > "Howard" <> wrote in message

    news:be1oe2$...
    >
    > > Iterators like that above are essentially pointers, aren't they?

    >
    > Not for lists they aren't. Vectors can get away with pointers, but
    > how is ++ going to do something intelligent for lists unless it can
    > be overloaded, which would mean it has to be a class.
    >
    >

    Ah. Thanks...I haven't used lists yet, only vectors, so I wasn't aware of
    the difference. Makes sense, though, since ++ has to accomplish what I'd
    normally do with a "next" pointer in a linked list algorithm.

    To the OP: sorry for the misleading info.

    -Howard
    Howard, Jul 3, 2003
    #6
  7. sks_cpp

    Dhruv Guest

    On Thu, 03 Jul 2003 17:05:24 +0000, sks_cpp wrote:

    > Consider the following:
    >
    > list<int> a; // assume a contains some number of integers
    >

    Ok, populate list.

    > std::list<int>::iterator iter = a.find(10);

    Should be: ... = std::find (a.begin(), a.end(), 10);

    Make these corrections too:
    > for (/*iter*/; iter /*<*/ != a.end(); ++iter)
    > {
    > std::cout << "found one\n";
    > }


    Why not use:

    iter = a.begin();
    while (iter != end()) {
    iter = std::find (iter, a.end(), 10); ++iter;
    std::cout<<"Found one"<<std::endl; }



    > ================
    > Where are all the memory allocations here?


    There are no memory allocations.
    They have all been assumed by you when you said "assume the list is full".


    > There is one for the "iter". Does the find allocate any memory in order to
    > create the iterator?
    > I suppose the ++iter does?


    Nope, none of them do any such thing.


    Regards,
    -Dhruv.
    Dhruv, Jul 3, 2003
    #7
  8. sks_cpp

    Jerry Coffin Guest

    In article <onZMa.369668$>,
    says...
    > Consider the following:
    >
    > list<int> a; // assume a contains some number of integers
    >
    > list<int>::iterator iter = a.find(10);
    > for (iter; iter < a.end(); ++iter)
    > {
    > std::cout << "found one\n";
    > }


    Many of the comments you've received have had little to do with the
    questions you asked -- this is worse: it has _nothing_ to do with the
    questions you asked! <G>

    I'd use something like this:

    void show() {
    std::cout << "found one\n";
    }

    std::for_each(a.find(10), a.end(), show);

    Remember: there's a reason for_each (among other things) takes two
    iterators instead of just the name of a container -- it isn't just to
    irritate you, but to allow for situations like this, where you want to
    start iterating from somewhere in the middle of a collection instead of
    from the beginning.

    You might also want to consider the boost::lambda library to obviate
    creating the show() function.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jul 4, 2003
    #8
    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. Asfand Yar Qazi
    Replies:
    4
    Views:
    382
    Asfand Yar Qazi
    Nov 12, 2004
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    475
    Kai-Uwe Bux
    May 8, 2005
  3. Martin M.
    Replies:
    4
    Views:
    333
    Simon Brunning
    Dec 15, 2005
  4. , India
    Replies:
    10
    Views:
    1,055
    James Kanze
    Aug 8, 2009
  5. Joshua Maurice
    Replies:
    2
    Views:
    244
    Brian Wood
    Sep 23, 2010
Loading...

Share This Page