Sequence points

Discussion in 'C++' started by Senthil, Dec 1, 2005.

  1. Senthil

    Senthil Guest

    Hi,

    When standard states this
    "
    i = v[i++]; // the behavior is undefined
    i = ++i + 1; // the behavior is undefined
    "

    Do both of the following code snippets produces undefined behaviour or
    the second one alone.

    int m = 2;
    int answer = ++m * ++m + ++m * ++m;

    or

    int m = 2
    m = ++m * ++m + ++m * ++m;

    What are the sequence points inside these expressions?

    Thanks,
    Senthil
     
    Senthil, Dec 1, 2005
    #1
    1. Advertising

  2. Senthil

    Artie Gold Guest

    Senthil wrote:
    > Hi,
    >
    > When standard states this
    > "
    > i = v[i++]; // the behavior is undefined
    > i = ++i + 1; // the behavior is undefined
    > "
    >
    > Do both of the following code snippets produces undefined behaviour or
    > the second one alone.
    >
    > int m = 2;
    > int answer = ++m * ++m + ++m * ++m;
    >
    > or
    >
    > int m = 2
    > m = ++m * ++m + ++m * ++m;
    >
    > What are the sequence points inside these expressions?
    >
    > Thanks,
    > Senthil
    >

    They both produce UB, as there *are* no sequence points in either
    expression.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Dec 1, 2005
    #2
    1. Advertising

  3. Senthil

    Pete Becker Guest

    Senthil wrote:
    >
    > What are the sequence points inside these expressions?
    >


    There aren't any. There is a sequence point at the end of each one. The
    behavior of both statements is undefined, because they modify the value
    of m more than once without an intervening sequence point.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Dec 1, 2005
    #3
  4. Senthil

    Senthil Guest

    Thanks Pete and Artie!!!

    I read books and i read the standard..but i am still not able to
    understand what a sequence point is ?
    :(

    Greetings,
    Senthil
     
    Senthil, Dec 2, 2005
    #4
  5. Senthil

    Jack Klein Guest

    On 1 Dec 2005 19:56:14 -0800, "Senthil" <> wrote
    in comp.lang.c++:

    > Thanks Pete and Artie!!!
    >
    > I read books and i read the standard..but i am still not able to
    > understand what a sequence point is ?
    > :(
    >
    > Greetings,
    > Senthil


    Sequence points:

    1. The ';' at the end of a full expression.

    2. The comma operator (but the commas that separate arguments in a
    function call are NOT comma operators).

    3. At the '?' in the ternary expression.

    4. During a function call, after all the arguments are evaluated,
    before the function begins executing.

    5. When a function ends/returns or exits by throwing an exception.

    6. At the evaluation of a && or || logical operator.

    7. At the initialization of each base and member in a constructor
    with an initialization list.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 2, 2005
    #5
  6. Senthil

    Senthil Guest

    Thanks a ton, Jack


    -senthil
     
    Senthil, Dec 2, 2005
    #6
    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. Dave Theese

    Sequence points

    Dave Theese, Sep 3, 2003, in forum: C++
    Replies:
    2
    Views:
    353
    Karl Heinz Buchegger
    Sep 3, 2003
  2. Timothy Madden

    Does C++ have sequence points ?

    Timothy Madden, Sep 20, 2004, in forum: C++
    Replies:
    4
    Views:
    420
    Peter van Merkerk
    Sep 21, 2004
  3. j

    Sequence points

    j, Jul 23, 2003, in forum: C Programming
    Replies:
    4
    Views:
    390
    Tak-Shing Chan
    Jul 24, 2003
  4. akarl

    Knowing your sequence points

    akarl, Aug 11, 2005, in forum: C Programming
    Replies:
    7
    Views:
    399
    Jack Klein
    Aug 12, 2005
  5. Question about sequence points

    , Aug 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    285
    Clark S. Cox III
    Aug 12, 2006
Loading...

Share This Page