Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iterators - do they allocate memory?

Reply
Thread Tools

iterators - do they allocate memory?

 
 
sks_cpp
Guest
Posts: n/a
 
      07-03-2003
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.


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      07-03-2003

"sks_cpp" <(E-Mail Removed)> wrote in message newsnZMa.369668$(E-Mail Removed). ..
> 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)


 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      07-03-2003

"sks_cpp" <(E-Mail Removed)> wrote in message
newsnZMa.369668$(E-Mail Removed). ..
> 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      07-03-2003

"Howard" <(E-Mail Removed)> wrote in message news:be1oe2$(E-Mail Removed)...

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


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      07-03-2003
On Thu, 03 Jul 2003 17:05:24 GMT, "sks_cpp" <(E-Mail Removed)>
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
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      07-03-2003

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:3f04667d$0$87909$(E-Mail Removed) m...
>
> "Howard" <(E-Mail Removed)> wrote in message

news:be1oe2$(E-Mail Removed)...
>
> > 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



 
Reply With Quote
 
Dhruv
Guest
Posts: n/a
 
      07-03-2003
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.






 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      07-04-2003
In article <onZMa.369668$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
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
Re: iterators in sets - should they be const? Joshua Maurice C++ 2 09-23-2010 12:56 AM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
MCSE exam # 70-216. They they show you the score?? Jack Black Microsoft Certification 1 12-09-2004 11:54 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments