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

L

lujnan

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.
 
R

Ruben Mikkonen

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.
 
B

bert

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.
--
 
L

lujnan

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.
 
S

Slash QB

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.
 
K

Keith Thompson

Ruben Mikkonen said:
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".
 
J

Joe Pfeiffer

lujnan said:
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.
 
J

Joe Pfeiffer

Keith Thompson said:
Ruben Mikkonen said:
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).
 
S

Shao Miller

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.
 
B

Barry Schwarz

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.
 
K

Kaz Kylheku

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.
 
K

Kaz Kylheku

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.
 
L

lujnan

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.
 
N

Nick Keighley

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)
 
J

James Kuyper

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.
 
K

Keith Thompson

lujnan said:
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.
 
K

Kaz Kylheku

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 ...
 
B

Barry Schwarz

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?
 
K

Kaz Kylheku

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.
 
B

Ben Pfaff

Kaz Kylheku said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top