what is the right value about a = a++;

Discussion in 'C Programming' started by lujnan, Feb 6, 2012.

  1. lujnan

    lujnan Guest

    in GCC and VS2008
    the fllow code

    int a = 0, b;
    b = a = a++;
    printf("%d, %d\n", a, b);

    result is : 1, 0

    in Java

    they are all 0.
    lujnan, Feb 6, 2012
    #1
    1. Advertising

  2. Re: what is the right value about a = a++;

    On 6 helmi, 10:10, lujnan <> wrote:
    > in GCC and VS2008
    > the fllow code
    >
    > int a = 0, b;
    > b = a = a++;
    > printf("%d, %d\n", a, b);
    >
    > result is : 1, 0
    >
    > in Java
    >
    > they are all 0.



    I think that a = a++ means
    the following: first a = a,
    and then you increase a by 1.
    So in my opinion, the
    statement should equal to
    a = a+1, or a++.

    However, in the statement
    b = a = a++, the case is
    that first you insert a to
    be and then a++ to a (a to
    a and then increase it by
    1). Thus, b equals 0 and
    a equals 1.

    From the aforementioned
    follows that Java sucks
    and GCC sucks less.
    Ruben Mikkonen, Feb 6, 2012
    #2
    1. Advertising

  3. lujnan

    bert Guest

    On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    > b = a = a++;


    The question is posted in this group many times.
    Read the FAQ. The result is undefined - that is,
    it is DEFINED to be undefined. That means it is
    okay for the result to be different in different
    compilers, and it is even okay for it to be
    different in the same compiler in different
    places or at different times.
    --
    bert, Feb 6, 2012
    #3
  4. lujnan

    lujnan Guest

    Re: what is the right value about a = a++;

    On Feb 6, 5:45 pm, bert <> wrote:
    > On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    > > b = a = a++;

    >
    > The question is posted in this group many times.
    > Read the FAQ.  The result is undefined - that is,
    > it is DEFINED to be undefined.  That means it is
    > okay for the result to be different in different
    > compilers, and it is even okay for it to be
    > different in the same compiler in different
    > places or at different times.
    > --


    maybe it shows the c world is not perfect. in this point i like the
    define of java.
    Why C Standards Committee does not constrain this case.
    lujnan, Feb 6, 2012
    #4
  5. lujnan

    Slash QB Guest

    Re: what is the right value about a = a++;


    > b = a = a++;
    > printf("%d, %d\n", a, b);


    a=a++ or a=++a
    The above statement when compiled results different in different
    compilers.
    So, such expressions are undefined.
    Slash QB, Feb 6, 2012
    #5
  6. Re: what is the right value about a = a++;

    Ruben Mikkonen <> writes:
    [...]
    > I think that a = a++ means the following: first a = a, and then you
    > increase a by 1. So in my opinion, the statement should equal to a =
    > a+1, or a++.

    [...]

    This is not a matter of opinion. As has been stated countless times,
    the behavior is undefined -- not just the result of the expression,
    or the values assigned to a, but the behavior of any program that
    evaluates that expression. It can do exactly what you expect it
    to do, it can crash your program, it can reformat your hard drive.

    What "a = a++" really means is that the person who wrote it
    does not understand C well enough, and has not read the FAQ at
    <http://c-faq.com>. (Both problems are curable).

    Whatever you intended to do, there's certainly a better way to
    express it. If you just want to increment a, just write "a++";
    that already assigns the new value to a, so there's no need for an
    assignment operator in addition to that. The "++" operator is not
    just a fancy way of writing "+ 1".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 7, 2012
    #6
  7. lujnan

    Joe Pfeiffer Guest

    lujnan <> writes:

    > in GCC and VS2008
    > the fllow code
    >
    > int a = 0, b;
    > b = a = a++;
    > printf("%d, %d\n", a, b);
    >
    > result is : 1, 0
    >
    > in Java
    >
    > they are all 0.


    Keep in mind C and Java are not the same language, so you can't expect
    them to have the same semantics. The results you got for the C
    compilers are correct (for C). I would assume the result you got for
    Java is also correct (for Java), but I don't use Java so I don't really
    know.
    Joe Pfeiffer, Feb 7, 2012
    #7
  8. lujnan

    Joe Pfeiffer Guest

    Re: what is the right value about a = a++;

    Keith Thompson <> writes:

    > Ruben Mikkonen <> writes:
    > [...]
    >> I think that a = a++ means the following: first a = a, and then you
    >> increase a by 1. So in my opinion, the statement should equal to a =
    >> a+1, or a++.

    > [...]
    >
    > This is not a matter of opinion. As has been stated countless times,
    > the behavior is undefined -- not just the result of the expression,
    > or the values assigned to a, but the behavior of any program that
    > evaluates that expression. It can do exactly what you expect it
    > to do, it can crash your program, it can reformat your hard drive.
    >
    > What "a = a++" really means is that the person who wrote it
    > does not understand C well enough, and has not read the FAQ at
    > <http://c-faq.com>. (Both problems are curable).
    >
    > Whatever you intended to do, there's certainly a better way to
    > express it. If you just want to increment a, just write "a++";
    > that already assigns the new value to a, so there's no need for an
    > assignment operator in addition to that. The "++" operator is not
    > just a fancy way of writing "+ 1".


    Urrgh. I stand corrected (in the past I've made fruitless tries at
    cancelling posts I've made that are wrong; since there's no real point
    I'll just recognize that I was wrong now, and ignore the corrections to
    my post that are sure to follow).
    Joe Pfeiffer, Feb 7, 2012
    #8
  9. lujnan

    Shao Miller Guest

    On 2/6/2012 03:10, lujnan wrote:
    > in GCC and VS2008
    > the fllow code
    >
    > int a = 0, b;
    > b = a = a++;
    > printf("%d, %d\n", a, b);
    >
    > result is : 1, 0
    >
    > in Java
    >
    > they are all 0.
    >


    In this context, your:

    a++

    expression is similar to the expression:

    ((a = a + 1), (a - 1))

    except that there is no sequence point in 'a++' and there is only one
    read access in 'a++' and one write access in 'a++'.

    Behold the following events for the following expressions:

    a++

    A.1. Read the value of 'a' before the next sequence point
    A.2. Increment the value of 'a' (a write) before the next sequence point

    a = ...

    B.1. Evaluate the expression '...' before the next sequence point
    B.2. Store that value to 'a' before the next sequence point

    b = ...

    C.1. Evaluate the expression '...' before the next sequence point
    C.2. Store that value to 'b' before the next sequence point

    Note the ordering.

    So we get the following order for your special line:

    b = a = a++;

    1. Evaluate the expression 'a = a++' (C.1)
    2. Evaluate the expression 'a++' (B.1)
    3. Read the value of 'a' (A.1)
    ?. Any time now or before the next sequence point, increment the value
    of 'a' (A.2)
    ?. Any time now or before the next sequence point, store the value to
    'a' (B.2)
    4. There's no time left before the next sequence point, so we must store
    the value to 'b' (C.2)

    Note how #4 is ordered relative to #1, #2, #3, but unordered relative to
    the two '?' operations. The '?' operations and #4 must occur after #3,
    but can occur in any order.

    Since there are two "write" operations to 'a' that can potentially occur
    at the same time, that results in undefined behaviour.

    Maybe the increment of 'a' happens first. Maybe the other store to 'a'
    happens first. Maybe they happen at exactly the same time and the
    computer crashes. Other "maybes" are possible too, since there are no
    defined limits to what can happen.
    Shao Miller, Feb 7, 2012
    #9
  10. Re: what is the right value about a = a++;

    On Mon, 6 Feb 2012 02:33:24 -0800 (PST), lujnan <>
    wrote:

    >On Feb 6, 5:45 pm, bert <> wrote:
    >> On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    >> > b = a = a++;

    >>
    >> The question is posted in this group many times.
    >> Read the FAQ.  The result is undefined - that is,
    >> it is DEFINED to be undefined.  That means it is
    >> okay for the result to be different in different
    >> compilers, and it is even okay for it to be
    >> different in the same compiler in different
    >> places or at different times.
    >> --

    >
    >maybe it shows the c world is not perfect. in this point i like the
    >define of java.
    >Why C Standards Committee does not constrain this case.


    They did. They put explicit text in the language standard that
    specifically states that this type of expression produces undefined
    behavior.

    Since one group of people with significant input to the standard is
    the compiler writers, the committee accepted their assertion that
    forcing the compiler to diagnose most kinds of undefined behavior is
    just not practical.

    What more would you want and how much are you willing to pay for it?
    Not just initial money outlay but also extended compile times,
    additional diagnostics that require documentation, etc. Since some
    forms of undefined behavior can only be detected during execution, you
    will also have larger program sizes and slower execution speeds. "Be
    careful what you wish for" is good advice.

    --
    Remove del for email
    Barry Schwarz, Feb 7, 2012
    #10
  11. lujnan

    Kaz Kylheku Guest

    On 2012-02-07, Joe Pfeiffer <> wrote:
    > Keep in mind C and Java are not the same language, so you can't expect
    > them to have the same semantics. The results you got for the C
    > compilers are correct (for C). I would assume the result you got for
    > Java is also correct (for Java), but I don't use Java so I don't really
    > know.


    The result is correct for Java, which as a well-defined evaluation order. For C,
    there is no "correct" result. An expected result is not a correct result.
    Kaz Kylheku, Feb 7, 2012
    #11
  12. lujnan

    Kaz Kylheku Guest

    Re: what is the right value about a = a++;

    On 2012-02-07, Barry Schwarz <> wrote:
    > On Mon, 6 Feb 2012 02:33:24 -0800 (PST), lujnan <>
    > wrote:
    >
    >>On Feb 6, 5:45 pm, bert <> wrote:
    >>> On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    >>> > b = a = a++;
    >>>
    >>> The question is posted in this group many times.
    >>> Read the FAQ.  The result is undefined - that is,
    >>> it is DEFINED to be undefined.  That means it is
    >>> okay for the result to be different in different
    >>> compilers, and it is even okay for it to be
    >>> different in the same compiler in different
    >>> places or at different times.
    >>> --

    >>
    >>maybe it shows the c world is not perfect. in this point i like the
    >>define of java.
    >>Why C Standards Committee does not constrain this case.

    >
    > They did. They put explicit text in the language standard that
    > specifically states that this type of expression produces undefined
    > behavior.
    >
    > Since one group of people with significant input to the standard is
    > the compiler writers, the committee accepted their assertion that
    > forcing the compiler to diagnose most kinds of undefined behavior is
    > just not practical.


    I don't want diagnostics; I want strict left to right evaluation order
    of operands and subexpressions that complete their side effects before
    yielding a value.

    Making it unspecified and then trying to catch violations is an
    example of silly make-work: creating complexity and then trying
    to conquer it, for no benefit.

    This unspecified order of evaluation is a throwback to 1980,
    when *dst++ = *src++ generated better code than *dst = *src; src++; dst++.

    Once upon a time, you had to stuff multiple side effects into the same
    expression to get better code because the compiler was following some
    naive syntax-directed translation scheme.

    > forms of undefined behavior can only be detected during execution, you
    > will also have larger program sizes and slower execution speeds.


    Wouldn't /that/ be dumb, given that the (now false) rationale for this
    unspecified evaluation order business is to get faster code without
    working hard in the compiler.
    Kaz Kylheku, Feb 7, 2012
    #12
  13. lujnan

    lujnan Guest

    Re: what is the right value about a = a++;


    > I don't want diagnostics; I want strict left to right evaluation order
    > of operands and subexpressions that complete their side effects before
    > yielding a value.


    i agree with your point of view.
    lujnan, Feb 7, 2012
    #13
  14. Re: what is the right value about a = a++;

    On Feb 6, 10:35 am, Slash QB <> wrote:
    > > b = a = a++;
    > > printf("%d, %d\n", a, b);

    >
    > a=a++ or a=++a
    > The above statement when compiled results different in different
    > compilers.
    > So, such expressions are undefined.


    isn't that the other way round

    the statement is undefined
    =>
    compilers are permitted to produce different answers
    (or trap or crash or anything else weird the compiler writer feels
    like)
    Nick Keighley, Feb 7, 2012
    #14
  15. lujnan

    James Kuyper Guest

    Re: what is the right value about a = a++;

    On 02/06/2012 05:33 AM, lujnan wrote:
    > On Feb 6, 5:45�pm, bert <> wrote:
    >> On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    >>> b = a = a++;

    >>
    >> The question is posted in this group many times.
    >> Read the FAQ. �The result is undefined - that is,
    >> it is DEFINED to be undefined. �That means it is
    >> okay for the result to be different in different
    >> compilers, and it is even okay for it to be
    >> different in the same compiler in different
    >> places or at different times.
    >> --

    >
    > maybe it shows the c world is not perfect. in this point i like the
    > define of java.
    > Why C Standards Committee does not constrain this case.


    Because they don't want you to write code like this. It serves no useful
    purpose to write code this way. I can think of two plausible defined
    behaviors for a=a++. One is equivalent to a++, so why bother with the
    "a="? The other is equivalent to a = a, so why bother writing anything
    at all?

    The rule violated by this code is much more general than that, and
    includes cases that cannot be so easily dismissed. But it is almost
    always the case that the intended behavior of any code violating that
    rule can be more clearly expressed by separating it into two or more
    separate statements, none of which violate that rule.
    --
    James Kuyper
    James Kuyper, Feb 7, 2012
    #15
  16. Re: what is the right value about a = a++;

    lujnan <> writes:
    >> I don't want diagnostics; I want strict left to right evaluation order
    >> of operands and subexpressions that complete their side effects before
    >> yielding a value.

    >
    > i agree with your point of view.


    Then you don't want C.

    There are valid reasons for the way C leaves some expression evaluations
    undefined. It gives compilers freedom to perform certain optimizations,
    which makes for faster code. And code whose behavior is undefined tends
    to be unclear, and would better be written in a different way whose
    behavior is well defined.

    Even in a language that defines its behavior, "a = a++" is bad code.


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 7, 2012
    #16
  17. lujnan

    Kaz Kylheku Guest

    Re: what is the right value about a = a++;

    On 2012-02-07, James Kuyper <> wrote:
    > On 02/06/2012 05:33 AM, lujnan wrote:
    >> On Feb 6, 5:45�pm, bert <> wrote:
    >>> On Monday, February 6, 2012 8:10:00 AM UTC, lujnan wrote:
    >>>> b = a = a++;
    >>>
    >>> The question is posted in this group many times.
    >>> Read the FAQ. �The result is undefined - that is,
    >>> it is DEFINED to be undefined. �That means it is
    >>> okay for the result to be different in different
    >>> compilers, and it is even okay for it to be
    >>> different in the same compiler in different
    >>> places or at different times.
    >>> --

    >>
    >> maybe it shows the c world is not perfect. in this point i like the
    >> define of java.
    >> Why C Standards Committee does not constrain this case.

    >
    > Because they don't want you to write code like this. It serves no useful
    > purpose to write code this way.


    That particular instance (a = a++) serves no useful purpose. The instance
    does not damn the class.

    Does it serve no purpose that if I have f(..., g(), ... h()); I can
    be assured that g is called before h?

    Etc.

    Even if a = a++ serves no useful purpose, suppose that I'm managing a
    software project and someone writes that somewhere (not me, I wouldn't).
    It sure would help me if a = a++ were to be reliable and compute the same
    thing regardless of compiler, optimization setting or target.


    > I can think of two plausible defined
    > behaviors for a=a++. One is equivalent to a++, so why bother with the
    > "a="? The other is equivalent to a = a, so why bother writing anything
    > at all?


    One particular choice of behavior stands out: the simple, recursive rule:

    - evaluate each subexpression completely, includiing all of its side effects,
    before evaluating the parent expression.

    - if an expression has several subexpressions, evaluate them left to right.

    > But it is almost
    > always the case that the intended behavior of any code violating that
    > rule can be more clearly expressed by separating it into two or more
    > separate statements, none of which violate that rule.


    That's obviously only if you know what the behavior should be.

    That's easy; just find out the whereabouts of the guy who wrote it
    three years ago who no longer works here ...
    Kaz Kylheku, Feb 7, 2012
    #17
  18. Re: what is the right value about a = a++;

    On Tue, 7 Feb 2012 00:31:05 -0800 (PST), lujnan <>
    wrote:

    >
    >> I don't want diagnostics; I want strict left to right evaluation order
    >> of operands and subexpressions that complete their side effects before
    >> yielding a value.

    >
    >i agree with your point of view.


    Do you drive your car into lakes? Do you use a screwdriver as a
    soldering iron? Then why are you using a programming tool that
    doesn't do what you want when you already know of one that does?

    --
    Remove del for email
    Barry Schwarz, Feb 7, 2012
    #18
  19. lujnan

    Kaz Kylheku Guest

    Re: what is the right value about a = a++;

    On 2012-02-07, Keith Thompson <> wrote:
    > lujnan <> writes:
    >>> I don't want diagnostics; I want strict left to right evaluation order
    >>> of operands and subexpressions that complete their side effects before
    >>> yielding a value.

    >>
    >> i agree with your point of view.

    >
    > Then you don't want C.


    You could say that to anyone who is working on the next standard.

    Those who worked on C11 didn't want C99, and those who worked on C99
    didn't want C90.

    > There are valid reasons for the way C leaves some expression evaluations
    > undefined.


    No, there are not. There are only outdated beliefs rooted in computer
    science shamanism.

    Fact is, defined order is safe. Safety is the number one goal in engineering.

    > It gives compilers freedom to perform certain optimizations,


    No, it allows *programmers* to perform optimizations, when using
    compilers from early 1980-something.

    > which makes for faster code. And code whose behavior is undefined tends


    Modern compilers can rearrange code substantially, on a large scale,
    way beyond the space between two sequence points.

    > to be unclear, and would better be written in a different way whose


    It's only unclear because it is undefined. If you have a rule like
    subexpressions are evaluated from left to right, before parent expressions,
    and each expression is fully evaluated before the next expression,
    then all these expressions become perspiciously clear. Let's take
    a = i++. The main operator is =, so it is evaluated last.
    a is evaluated first because it is on the left. Then i++ is evaluated,
    producing the prior value of i. Then that value is moved into a
    (the original a of course: once a is evaluated it ceases
    to be a moving target.)

    > behavior is well defined.


    Yes, would better be. Would, should does not add up to will.

    Code will not rewrite itself.

    That ambiguous expression is sitting in the codebase somewhere, and you don't
    even know.

    And the intent? It died with the guy who left the company several years ago and
    then got run over by a bus.

    You're out of your intellectual league here, Kiki.
    Kaz Kylheku, Feb 7, 2012
    #19
  20. lujnan

    Ben Pfaff Guest

    Re: what is the right value about a = a++;

    Kaz Kylheku <> writes:

    > I don't want diagnostics; I want strict left to right evaluation order
    > of operands and subexpressions that complete their side effects before
    > yielding a value.


    You could use Java.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Feb 7, 2012
    #20
    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. Tom
    Replies:
    0
    Views:
    423
  2. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    982
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  3. Bogdan
    Replies:
    1
    Views:
    783
    Bogdan
    Jun 16, 2009
  4. Leon
    Replies:
    3
    Views:
    169
    TaeHo Yoo
    Nov 26, 2004
  5. Oran
    Replies:
    2
    Views:
    532
Loading...

Share This Page