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++;

 
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-07-2012
On 2012-02-07, Joe Pfeiffer <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-07-2012
On 2012-02-07, Barry Schwarz <(E-Mail Removed)> wrote:
> 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.


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.
 
Reply With Quote
 
 
 
 
lujnan
Guest
Posts: n/a
 
      02-07-2012

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

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-07-2012
On Feb 6, 10:35*am, Slash QB <(E-Mail Removed)> 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)
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-07-2012
On 02/06/2012 05:33 AM, lujnan 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.


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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-07-2012
lujnan <(E-Mail Removed)> 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.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
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-07-2012
On 2012-02-07, James Kuyper <(E-Mail Removed)> wrote:
> On 02/06/2012 05:33 AM, lujnan 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.

>
> 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 ...
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-07-2012
On Tue, 7 Feb 2012 00:31:05 -0800 (PST), lujnan <(E-Mail Removed)>
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
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-07-2012
On 2012-02-07, Keith Thompson <(E-Mail Removed)> wrote:
> lujnan <(E-Mail Removed)> 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] = i++. The main operator is =, so it is evaluated last.
a[i] 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[i]
(the original a[i] of course: once a[i] 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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-07-2012
Kaz Kylheku <(E-Mail Removed)> 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,0xa6 7f6aaa,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 1utchar(a[i&15]);break;}}}
 
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