Dereferencing past-the-end iterators

Discussion in 'C++' started by Matthias Kaeppler, Jul 22, 2005.

  1. Hello,

    I was wondering, does dereferencing past-the-end
    iterators yield undefined behavior? Especially, is
    the result of calling an STL algorithm on an empty
    range undefined?

    For example (pseudocode):

    begin = find(...);
    end = find(...);

    sort(begin, find, pred);

    If begin is a past-the-end iterator, is the call
    to sort() an unsafe operation? Is it good practice
    to check iterators for being valid before passing
    them to an STL algo?

    Regards,
    Matthias
    Matthias Kaeppler, Jul 22, 2005
    #1
    1. Advertising

  2. Matthias Kaeppler wrote:

    > I was wondering, does dereferencing past-the-end
    > iterators yield undefined behavior?


    Yes.

    > Especially, is
    > the result of calling an STL algorithm on an empty
    > range undefined?


    No, that's well defined. It doesn't do anything. In particular,
    nothing gets dereferenced, so there's no undefined behavior.

    > For example (pseudocode):
    >
    > begin = find(...);
    > end = find(...);
    >
    > sort(begin, find, pred);
    >
    > If begin is a past-the-end iterator


    By "past-the-end iterator," I assume you in fact mean an iterator equal
    to end(). Given that it's name is "end()," it's confusing for you to
    refer to it as "past-the-end."

    > , is the call
    > to sort() an unsafe operation?


    No, it's safe, because nothing gets dereferenced if begin==end.

    > Is it good practice
    > to check iterators for being valid before passing
    > them to an STL algo?


    What do you mean by "valid"? An iterator equal to end() is valid, but
    you can't dereference it. And end() is frequently the second iterator
    passed to an algorithm. E.g.:

    std::sort(seq.begin(), seq.end());

    If seq is empty, then begin()==end(), there's nothing to sort, and
    nothing gets dereferenced by the call to sort(). You don't have to
    check for that.

    Best regards,

    Tom
    Thomas Tutone, Jul 22, 2005
    #2
    1. Advertising

  3. Thomas Tutone wrote:
    > If seq is empty, then begin()==end(), there's nothing to sort, and
    > nothing gets dereferenced by the call to sort(). You don't have to
    > check for that.


    Alright, that's what I was worried about. Thanks Tom.
    Matthias Kaeppler, Jul 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Aguilar
    Replies:
    4
    Views:
    3,625
    Lionel B
    May 9, 2005
  2. ozbear

    One-past-end-of-object pointers

    ozbear, Sep 1, 2004, in forum: C Programming
    Replies:
    3
    Views:
    467
  3. Old Wolf

    Subtracting 0 from one-past-end pointer

    Old Wolf, Nov 17, 2004, in forum: C Programming
    Replies:
    7
    Views:
    356
    Dan Pop
    Nov 17, 2004
  4. Mark Stijnman
    Replies:
    5
    Views:
    499
    Mark Stijnman
    Jan 28, 2006
  5. Nikos Chantziaras
    Replies:
    15
    Views:
    571
    Nikos Chantziaras
    Apr 14, 2011
Loading...

Share This Page