side effects

Discussion in 'C++' started by andrew browning, May 14, 2006.

  1. does i++ have the same potential for side effects as its pre-fix
    equivilent ++i? and if so, is it always dangerous to use them, as in a
    for loop, or only in certain circumstances?
     
    andrew browning, May 14, 2006
    #1
    1. Advertising

  2. andrew browning

    Alan Johnson Guest

    andrew browning wrote:
    > does i++ have the same potential for side effects as its pre-fix
    > equivilent ++i? and if so, is it always dangerous to use them, as in a
    > for loop, or only in certain circumstances?
    >


    Define "potential for side effects". Both ++i and i++ will increment i,
    if that is what you are asking (or in the case of class types, call the
    corresponding version of operator++).

    Also, specify what you mean by "dangerous to use them". It is quite
    common for increment operators to be used in for loops:
    for (unsigned i = 0; i < 10; ++i) { // Do something }

    The only dangerous use I can think of is trying to use an increment
    operator in an expression where the operand is also being used for
    something else:
    i = ++i + 1; // unspecified behavior
    j = i + i++; // unspecified behavior
    j = i++ + ++i; // unspecified behavior

    Also, since it inevitably will come up:
    http://www.parashift.com/c -faq-lite/operator-overloading.html#faq-13.15

    --
    Alan Johnson
     
    Alan Johnson, May 14, 2006
    #2
    1. Advertising

  3. * andrew browning:
    > does i++ have the same potential for side effects as its pre-fix
    > equivilent ++i?


    No, i++ is more side effect oriented than ++i because i++ has only one
    reason for existence: producing a side effect in an expression.

    i++ can be regarded as (temp = i, ++i, temp).


    > and if so, is it always dangerous to use them


    No.


    > as in a for loop, or only in certain circumstances?


    No. But prefer ++i. That way you tell the reader that you're not
    interested in the side effect, and it can be more efficient.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 14, 2006
    #3
  4. got it.

    am i too take it that i'm breaking usenet protocol somehow? i hope i
    am not:(
     
    andrew browning, May 14, 2006
    #4
  5. * andrew browning:
    > got it.
    >
    > am i too take it that i'm breaking usenet protocol somehow? i hope i
    > am not:(


    Well, now you are, because you forgot to quote what you responded to.
    But originally you were not. Heh. :)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 14, 2006
    #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. Jim Bancroft
    Replies:
    2
    Views:
    2,474
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. Anonieko
    Replies:
    0
    Views:
    1,982
    Anonieko
    Aug 12, 2005
  3. VisionSet
    Replies:
    7
    Views:
    487
    Michael Rauscher
    Nov 9, 2004
  4. Jason Heyes
    Replies:
    10
    Views:
    557
    red floyd
    Dec 21, 2004
  5. Giuseppe

    Re: Newbie-Side effects?

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,180
    Giuseppe
    Jun 24, 2003
Loading...

Share This Page