473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

A question about std::lists

I have a method which traverses a list and then clears it.
I was optimizing it and broke it.

series *Ser;
series::iterator im_it;

for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = *im_it;
...
}
Ser->clear();

vs. (Which dies a horrible death on the last element.)

for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
{
img = *im_it;
...
}

Aug 25 '05 #1
6 1273
JustSomeGuy wrote:
I have a method which traverses a list and then clears it.
I was optimizing it and broke it.

series *Ser;
series::iterator im_it;

for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = *im_it;
...
}
Ser->clear();

vs. (Which dies a horrible death on the last element.)

for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
{
img = *im_it;
...
}


I can't see how thats an optimisation.

Presumably you still have to increment your iterator somewhere.

Perhaps you should use the dereferenced iterator, rather than copying it when you do:

img = *im_it;

I fail to see why you would copy it, but then, I fail to see most of your code, also.

Ben
--
I'm not just a number. To many, I'm known as a String...
Aug 25 '05 #2
JustSomeGuy wrote:
I have a method which traverses a list and then clears it.
I was optimizing it and broke it.

series *Ser;
series::iterator im_it;

for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = *im_it;
...
}
Ser->clear();

vs. (Which dies a horrible death on the last element.)

for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
{
img = *im_it;
Shouldn't this be

img = *im_it++;

?
...
}


Since you posted a mock-up instead of the actual code, there is
no way to tell what the reason for undefined behaviour was.

V
Aug 25 '05 #3
"JustSomeGuy" <bo*******@yahoo.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
I have a method which traverses a list and then clears it.
I was optimizing it and broke it.

series *Ser;
series::iterator im_it;

for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = *im_it;
...
}
Ser->clear();

vs. (Which dies a horrible death on the last element.)

for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
{
img = *im_it;
...
}


I'm somewhat surprised that it didn't blow up on the first element.
You've invoked undefined behaviour by destroying the object that the
iterator refers to. Let's rewrite your for loop to its equivalent while
loop:

im_it=Ser->begin();

while (im_it != Ser->end())
{
img = *im_it;
...
Ser->pop_front();
}
So, before pop_front is called, im_it is an iterator to the first element
in the list. After calling pop_front, that iterator is now invalidated.
Attempting to use it for anything is undefined behaviour.

Probably a better optimization would be:

while (!Ser->emtpy())
{
img = Ser->front();
...
Ser->pop_front();
}

Aug 25 '05 #4
I wanted to free up the memory as soon as possible...

Aug 25 '05 #5

Andre Kostur wrote:
"JustSomeGuy" <bo*******@yahoo.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
I have a method which traverses a list and then clears it.
I was optimizing it and broke it.

series *Ser;
series::iterator im_it;

for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = *im_it;
...
}
Ser->clear();

vs. (Which dies a horrible death on the last element.)

for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
{
img = *im_it;
...
}


I'm somewhat surprised that it didn't blow up on the first element.
You've invoked undefined behaviour by destroying the object that the
iterator refers to. Let's rewrite your for loop to its equivalent while
loop:

im_it=Ser->begin();

while (im_it != Ser->end())
{
img = *im_it;
...
Ser->pop_front();
}
So, before pop_front is called, im_it is an iterator to the first element
in the list. After calling pop_front, that iterator is now invalidated.
Attempting to use it for anything is undefined behaviour.

Probably a better optimization would be:

while (!Ser->emtpy())
{
img = Ser->front();
...
Ser->pop_front();
}


I think calling Ser->clear() in the first place and forgetting about
the loop would be an even better optimization.

What exactly is the contribution that this loop is making to the
program?

Greg

Aug 25 '05 #6
"Greg" <gr****@pacbell.net> wrote in
news:11*********************@o13g2000cwo.googlegro ups.com:

Andre Kostur wrote:
"JustSomeGuy" <bo*******@yahoo.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
> I have a method which traverses a list and then clears it.
> I was optimizing it and broke it.
>
> series *Ser;
> series::iterator im_it;
>
> for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
> {
> img = *im_it;
> ...
> }
> Ser->clear();
>
> vs. (Which dies a horrible death on the last element.)
>
> for (im_it=Ser->begin(); im_it != Ser->end(); Ser->pop_front())
> {
> img = *im_it;
> ...
> }


I'm somewhat surprised that it didn't blow up on the first element.
You've invoked undefined behaviour by destroying the object that the
iterator refers to. Let's rewrite your for loop to its equivalent
while loop:

im_it=Ser->begin();

while (im_it != Ser->end())
{
img = *im_it;
...
Ser->pop_front();
}
So, before pop_front is called, im_it is an iterator to the first
element in the list. After calling pop_front, that iterator is now
invalidated. Attempting to use it for anything is undefined
behaviour.

Probably a better optimization would be:

while (!Ser->emtpy())
{
img = Ser->front();
...
Ser->pop_front();
}


I think calling Ser->clear() in the first place and forgetting about
the loop would be an even better optimization.

What exactly is the contribution that this loop is making to the
program?


Beats me. However, I think correcting the misusage of the iterator was
worth correcting. (I probably should have quoted the word "optimization")

Aug 25 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kristofer Pettijohn | last post by:
My question is about lists: Is there a way to remove duplicate items from a list that are next to each other? Example... Performing the operation on will return
2
by: JustSomeGuy | last post by:
I have a few classes... class a : std::list<baseclass> { int keya; }; class b : std::list<a> { int keyb;
3
by: Jochus | last post by:
Hi! Today we saw the information about lists. We have an assignment about CGI. That's all going well, but we're stuck at the insertion sorft of linked lists -- void voeg_in_lijst(const char*...
2
by: Christian Chrismann | last post by:
Hi, an application uses a wrapper class for an STL list. Basically, this template class provides the same functions (delete, append, find...) as an ordinary STL list. Additionally, there are...
5
by: Scott | last post by:
I'm sorry if most of my question's seem "petty", but as I've said before, I need to know the petty just because I need to know. This question is more along the lines of just having you guys...
9
by: sake | last post by:
Hi, Just a quick question about lists. This seemed a bit elementary at first, but nothing turned up on the web: How do I get a linked list in C++ with multiple elements. I need to copy this sort...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
3
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.