473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterators - do they allocate memory?

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

"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

"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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1362
by: Chris Lyon | last post by:
coming from a Vb'ish sort of background, issues like iterators and list comprehensions appear at first site to be a confusion. However since I have a great deal of respect for the large brains in a...
4
2563
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
18
2273
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
24
3913
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
2
2330
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
8
2249
by: desktop | last post by:
In accelerated C++ on page 146 there is this example: template <class In, class Out> Out copy(In begin, In end, Out dest) { While (begin != end) *dest++ = *begin++; return dest; }
11
4142
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
2
2384
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type...
3
2740
by: zr | last post by:
Hi, Does usage of checked iterators and checked containers make code more secure? If so, can that code considered to be reasonably secure?
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.