Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what is the right value about a = a++;

Reply
Thread Tools

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

 
 
lujnan
Guest
Posts: n/a
 
      02-06-2012
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.


 
Reply With Quote
 
 
 
 
Ruben Mikkonen
Guest
Posts: n/a
 
      02-06-2012
On 6 helmi, 10:10, lujnan <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
bert
Guest
Posts: n/a
 
      02-06-2012
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.
--

 
Reply With Quote
 
lujnan
Guest
Posts: n/a
 
      02-06-2012
On Feb 6, 5:45*pm, bert <(E-Mail Removed)> 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.
 
Reply With Quote
 
Slash QB
Guest
Posts: n/a
 
      02-06-2012

> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-07-2012
Ruben Mikkonen <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      02-07-2012
lujnan <(E-Mail Removed)> 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.
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      02-07-2012
Keith Thompson <(E-Mail Removed)> writes:

> Ruben Mikkonen <(E-Mail Removed)> 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).
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-07-2012
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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-07-2012
On Mon, 6 Feb 2012 02:33:24 -0800 (PST), lujnan <(E-Mail Removed)>
wrote:

>On Feb 6, 5:45*pm, bert <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Menu Items is not right aligned with Right to Left documen =?Utf-8?B?QmlzaG95?= ASP .Net 0 12-28-2006 11:39 AM
Right-Click With Mouse and Toolba Buttonsr Don't Work Right Bigfoot It Is Computer Support 0 10-30-2006 06:08 PM
Tool to enable Right click on pages where Right click is disabled tsteinke@gmail.com Computer Support 4 08-28-2005 11:53 PM
Tool to right click image in windows explorer and rotate image right or left 90 degrees siliconpi Digital Photography 2 11-29-2004 12:56 PM
pass the right form input to the right control Tom ASP .Net 0 12-11-2003 03:07 AM



Advertisments