🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #3858

Re: C++ iterators

#3858
From: MaG
Date: Fri, 17 Oct 2003 21:26
47 lines
2692 bytes
"Gunter Schelfhout" <nospam@please.com> schreef in bericht
news:0eYjb.84164$jN3.4351093@phobos.telenet-ops.be...
> In het boek van Jossutis, The C++ Standard Library kom ik het volgend
> voorbeeld tegen (ingekort):
> ...
> vector<int> coll;
> // wat in de vector steken
> vector<int>::iterator pos;
> ...
> for (pos = coll.begin(); pos < coll.end(); pos += 2) {
>         //print
> }
>
> Over de bovenstaande for-loop zegt hij:
> "The expression += 2 might result in undefined behavior if it moves the
> iterator beyond the end() of the collection."
>
> Nu, ik zit dat hier te bekijken, maar ik zie het niet. Van zodra de
iterator
> voorbij het end() gaat, dan wordt dit toch opgevangen door het statement
> 'pos < coll.end()', of niet?
>
> Of zou het typo zijn en bedoelt hij 'pos != coll.end()'?
> -- 
> Blood, sweat & tears
>

1) eigenlijk hoor je geen bewerkingen uit te voeren op iterators, pos++ en
pos-- mag, de rest is gevaarlijk. (vergelijk het met pointer
bewerkingen)

2) de end memberfunktie wijst al 1 positie *na* het laatste item. Dus
wanneer pos naar het laatste item wijst is
pos < coll.end() nog steeds waar! En pos + 2 is dus  helemaal undefined!
D.w.z. kan vanalles zijn.
Die undefined zou wel eens weer kleiner dan coll.end() kunnen zijn! Dus die
for loop stopt niet gegarandeerd.
Bovendien trekt die for loop zich niets aan van wat er tussen de haken
gebeurt. Het middelste statement zal niet verhinderen
dat het derde deel iets illegaal uitvoert. De check gebeurt nadien.
pos != coll.end() lijkt mij ook beter, maar lost bovenstaand probleem niet
op.

De compiler maakt dus (gelukkig) terecht een opmerking.

MaG
Technical Details
Message-ID: <h8Zjb.2414$Wu5.276859050@hebe.telenet-ops.be>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsgate.cistron.nl!aotearoa.belnet.be!news.belnet.be!ossa.telenet-ops.be!hebe.telenet-ops.be.POSTED!not-for-mail
References: <0eYjb.84164$jN3.4351093@phobos.telenet-ops.be>

Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit
X-Google-Thread: fcea4,bfe50767efe7e7cd
X-Google-Attributes: gidfcea4,public
X-Google-ArrivalTime: 2003-10-17 14:28:48 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsgate.cistron.nl!aotearoa.belnet.be!news.belnet.be!ossa.telenet-ops.be!hebe.telenet-ops.be.POSTED!not-for-mail
From: "MaG" <nomail@nowhere.com>
Newsgroups: be.comp.programming
References: <0eYjb.84164$jN3.4351093@phobos.telenet-ops.be>
Subject: Re: C++ iterators
Lines: 49
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <h8Zjb.2414$Wu5.276859050@hebe.telenet-ops.be>
Date: Fri, 17 Oct 2003 21:26:37 GMT
NNTP-Posting-Host: 81.164.144.159
X-Complaints-To: abuse@telenet.be
X-Trace: hebe.telenet-ops.be 1066425997 81.164.144.159 (Fri, 17 Oct 2003 23:26:37 CEST)
NNTP-Posting-Date: Fri, 17 Oct 2003 23:26:37 CEST
Organization: Telenet Internet