does this invoke undefined behaviour

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 16, 2009.

  1. , India

    , India Guest

    I assign an initial iterator value returned by 'find()' algorithm to a
    variable called 'first'.
    Container::const_iterator first = find(c.begin(), c.end(), value);

    Then I want to find the next occurrence of the 'value' in the sequence
    to 'first'. To do this, I write the following code.

    if (first != c.end())
    {
    first = find(++first, c.end(), value);
    // do something with 'first'
    }

    Is the above assignment correct ? Or does it invoke undefined
    behaviour because I do an increment of 'first' inside 'find()'
    algorithm and store the result returned by 'find()' in 'first'
    itself ?

    Should I equivalently write the following piece of code ?

    Container::const_iterator temp;
    if (first != c.end())
    {
    temp = find(++first, c.end(), value);
    // do something with 'temp'
    }

    Kindly clarify.

    Thanks
    V.Subramanian
     
    , India, Nov 16, 2009
    #1
    1. Advertising

  2. , India

    Rolf Magnus Guest

    , India wrote:

    > I assign an initial iterator value returned by 'find()' algorithm to a
    > variable called 'first'.
    > Container::const_iterator first = find(c.begin(), c.end(), value);
    >
    > Then I want to find the next occurrence of the 'value' in the sequence
    > to 'first'. To do this, I write the following code.
    >
    > if (first != c.end())
    > {
    > first = find(++first, c.end(), value);
    > // do something with 'first'
    > }
    >
    > Is the above assignment correct ?


    Yes.

    > Or does it invoke undefined behaviour because I do an increment of 'first'
    > inside 'find()' algorithm and store the result returned by 'find()' in
    > 'first' itself ?


    No. Due to the function call, there is a sequence point in between.
     
    Rolf Magnus, Nov 16, 2009
    #2
    1. Advertising

  3. , India

    James Kanze Guest

    On Nov 16, 7:49 am, Rolf Magnus <> wrote:
    > , India wrote:
    > > I assign an initial iterator value returned by 'find()'
    > > algorithm to a variable called 'first'.
    > > Container::const_iterator first = find(c.begin(), c.end(), value);


    > > Then I want to find the next occurrence of the 'value' in
    > > the sequence to 'first'. To do this, I write the following
    > > code.


    > > if (first != c.end())
    > > {
    > > first = find(++first, c.end(), value);
    > > // do something with 'first'
    > > }


    > > Is the above assignment correct ?


    > Yes.


    > > Or does it invoke undefined behaviour because I do an
    > > increment of 'first' inside 'find()' algorithm and store the
    > > result returned by 'find()' in 'first' itself ?


    > No. Due to the function call, there is a sequence point in
    > between.


    On the other hand, modifying the same variable twice in a single
    expression is confusing to the reader. I'd probably use
    something like the Pascal succ function:

    template< typename Iterator >
    Iterator
    succ( Iterator iter )
    {
    ++ iter;
    return iter;
    }

    and write
    first = find( succ( first ), c.end(), value );

    (In practice, every time I've needed this, I've been dealing
    with string iterators, so I'd just write:
    first = find( first + 1, c.end(), value );
    No point in making things overly complicated when it doesn't buy
    you anything.)

    --
    James Kanze
     
    James Kanze, Nov 16, 2009
    #3
  4. , India

    Jeff Flinn Guest

    James Kanze wrote:
    > On Nov 16, 7:49 am, Rolf Magnus <> wrote:
    >> , India wrote:
    >>> I assign an initial iterator value returned by 'find()'
    >>> algorithm to a variable called 'first'.
    >>> Container::const_iterator first = find(c.begin(), c.end(), value);

    >
    >>> Then I want to find the next occurrence of the 'value' in
    >>> the sequence to 'first'. To do this, I write the following
    >>> code.

    >
    >>> if (first != c.end())
    >>> {
    >>> first = find(++first, c.end(), value);
    >>> // do something with 'first'
    >>> }

    >
    >>> Is the above assignment correct ?

    >
    >> Yes.

    >
    >>> Or does it invoke undefined behaviour because I do an
    >>> increment of 'first' inside 'find()' algorithm and store the
    >>> result returned by 'find()' in 'first' itself ?

    >
    >> No. Due to the function call, there is a sequence point in
    >> between.

    >
    > On the other hand, modifying the same variable twice in a single
    > expression is confusing to the reader. I'd probably use
    > something like the Pascal succ function:
    >
    > template< typename Iterator >
    > Iterator
    > succ( Iterator iter )
    > {
    > ++ iter;
    > return iter;
    > }
    >
    > and write
    > first = find( succ( first ), c.end(), value );
    >
    > (In practice, every time I've needed this, I've been dealing
    > with string iterators, so I'd just write:
    > first = find( first + 1, c.end(), value );
    > No point in making things overly complicated when it doesn't buy
    > you anything.)


    Or if you're already using boost that's spelled:

    #include <boost/next_prior.hpp>

    first = find( boost::next( first ), c.end(), value );

    Jeff
     
    Jeff Flinn, Nov 16, 2009
    #4
  5. , India

    James Kanze Guest

    On Nov 16, 1:53 pm, Jeff Flinn <> wrote:
    > James Kanze wrote:


    [...]
    > > On the other hand, modifying the same variable twice in a single
    > > expression is confusing to the reader. I'd probably use
    > > something like the Pascal succ function:


    > > template< typename Iterator >
    > > Iterator
    > > succ( Iterator iter )
    > > {
    > > ++ iter;
    > > return iter;
    > > }


    > > and write
    > > first = find( succ( first ), c.end(), value );


    > > (In practice, every time I've needed this, I've been dealing
    > > with string iterators, so I'd just write:
    > > first = find( first + 1, c.end(), value );
    > > No point in making things overly complicated when it doesn't buy
    > > you anything.)


    > Or if you're already using boost that's spelled:


    > #include <boost/next_prior.hpp>


    > first = find( boost::next( first ), c.end(), value );


    Except, of course, that you were probably using it long before
    you were using Boost, and can't be bothered to change.

    --
    James Kanze
     
    James Kanze, Nov 16, 2009
    #5
    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. ozbear

    Does this cause undefined behaviour?

    ozbear, Dec 30, 2003, in forum: C Programming
    Replies:
    14
    Views:
    553
    ozbear
    Dec 31, 2003
  2. David Resnick

    Does this invoke undefined behavior?

    David Resnick, Nov 11, 2004, in forum: C Programming
    Replies:
    1
    Views:
    371
    Dan Pop
    Nov 11, 2004
  3. Replies:
    5
    Views:
    265
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Dec 29, 2006
  4. Sharath A.V
    Replies:
    19
    Views:
    420
    Old Wolf
    Jul 27, 2006
  5. Prasoon Saurav
    Replies:
    1
    Views:
    541
Loading...

Share This Page