By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

iterators - do they allocate memory?

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"sks_cpp" <sk*****@hotmail.com> wrote in message
news:on********************@news2.central.cox.net. ..
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
Jul 19 '05 #2

P: n/a

"sks_cpp" <sk*****@hotmail.com> wrote in message news:on********************@news2.central.cox.net. ..
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)
Jul 19 '05 #3

P: n/a

"Howard" <al*****@hotmail.com> wrote in message news:be********@dispatch.concentric.net...
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.
Jul 19 '05 #4

P: n/a
On Thu, 03 Jul 2003 17:05:24 GMT, "sks_cpp" <sk*****@hotmail.com>
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
Jul 19 '05 #5

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Howard" <al*****@hotmail.com> wrote in message news:be********@dispatch.concentric.net...
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

Jul 19 '05 #6

P: n/a
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.


Jul 19 '05 #7

P: n/a
In article <on********************@news2.central.cox.net>,
sk*****@hotmail.com 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.
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.