iterators - do they allocate memory?

S

sks_cpp

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.
 
R

Ron Natalie

sks_cpp said:
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)
 
H

Howard

sks_cpp said:
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
 
R

Ron Natalie

Howard said:
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.
 
T

tom_usenet

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
 
H

Howard

Ron Natalie said:
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
 
D

Dhruv

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.
 
J

Jerry Coffin

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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top