Topic: Equality of default-constructed iterators


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/12/07
Raw View
Joseph Gottman wrote:
>
> Does the standard say anything about whether two STL iterators created using
> the default constructor and never assigned to are equal? ...

In general, iterators aren't even required required to be have default
constructors. However, forward iterators are required to have default
constructors, and reversible and bidirectional iterators inherit that
requirement. Therefore there are some iterators where the question is
meaningful. vector<>::iterator is required to be bidirectional, so it is
one of them.

> ...  Suppose I had some
> code like
>
> bool foo() {
>         vector<int>::iterator i, j // Could be any STL container
>         return (i == j);
> }
>
> Will foo always return true?  Could the statement i==j cause the program to
> crash?

No, and yes.
The value of a default-constructed iterator is allowed to be
singular(24.1.3 p1). In section 24.1 p5 it says "Results of most
expressions are undefined for singular values; the only exception is an
assignment of a non-singular value to an iterator that holds a singular
value."

This isn't merely theory. An ordinary pointer qualifies as a
bidirectional iterator, and some of the standard containers may use one
as such. An uninitialized pointer usually contains an effectively random
value. On some machines there are invalid addresses, which if actually
loaded into an address register, will cause the program to halt. This is
a deliberate safety feature: it's felt that any program careless enough
to load an invalid pointer is too poorly written to be safely executed.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Joseph Gottman" <joegottman@worldnet.att.net>
Date: 1999/12/04
Raw View
Does the standard say anything about whether two STL iterators created using
the default constructor and never assigned to are equal?  Suppose I had some
code like

bool foo() {
        vector<int>::iterator i, j // Could be any STL container
        return (i == j);
}

Will foo always return true?  Could the statement i==j cause the program to
crash?

Joe Gottman




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ark@research.att.com (Andrew Koenig)
Date: 1999/12/05
Raw View
In article <829r28$svf$1@bgtnsc02.worldnet.att.net>,
Joseph Gottman <joegottman@worldnet.att.net> wrote:

>Does the standard say anything about whether two STL iterators created using
>the default constructor and never assigned to are equal?

No it doesn't.

>Suppose I had some
>code like

>bool foo() {
>        vector<int>::iterator i, j // Could be any STL container
>        return (i == j);
>}

>Will foo always return true?

No.  Maybe it will; maybe it won't; maybe it will do something else
entirely.

>Could the statement i==j cause the program to
>crash?

Yes.
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]