Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16806 > unrolled thread

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

Started bylujnan <lujnan@gmail.com>
First post2012-02-06 00:10 -0800
Last post2012-02-07 01:18 -0500
Articles 14 on this page of 114 — 30 participants

Back to article view | Back to comp.lang.c


Contents

  what is the right value about  a = a++; lujnan <lujnan@gmail.com> - 2012-02-06 00:10 -0800
    Re: what is the right value about a = a++; Ruben Mikkonen <camu.umac@gmail.com> - 2012-02-06 00:28 -0800
      Re: what is the right value about a = a++; Keith Thompson <kst-u@mib.org> - 2012-02-06 21:27 -0800
        Re: what is the right value about a = a++; Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-02-06 22:39 -0700
    Re: what is the right value about  a = a++; bert <bert.hutchings@btinternet.com> - 2012-02-06 01:45 -0800
      Re: what is the right value about a = a++; lujnan <lujnan@gmail.com> - 2012-02-06 02:33 -0800
        Re: what is the right value about a = a++; "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-02-06 14:57 -0800
        Re: what is the right value about a = a++; Barry Schwarz <schwarzb@dqel.com> - 2012-02-06 22:50 -0800
          Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-07 08:15 +0000
            Re: what is the right value about a = a++; lujnan <lujnan@gmail.com> - 2012-02-07 00:31 -0800
              Re: what is the right value about a = a++; Keith Thompson <kst-u@mib.org> - 2012-02-07 09:07 -0800
                Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-07 17:31 +0000
                  Re: what is the right value about a = a++; Joshua Maurice <joshuamaurice@gmail.com> - 2012-02-09 17:50 -0800
                  Re: what is the right value about a = a++; Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-21 23:42 +0200
                    Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-22 04:28 +0000
                      Re: what is the right value about a = a++; Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-25 00:01 +0200
                      Re: what is the right value about a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-25 02:25 -0500
                        Re: what is the right value about a = a++; Ike Naar <ike@iceland.freeshell.org> - 2012-02-25 09:00 +0000
                          Re: what is the right value about a = a++; Philip Lantz <prl@canterey.us> - 2012-02-25 11:13 -0800
                            Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-25 19:34 +0000
                              Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-25 16:51 -0500
                          Re: what is the right value about a = a++; lawrence.jones@siemens.com - 2012-02-27 16:12 -0500
                            Re: what is the right value about a = a++; Ike Naar <ike@iceland.freeshell.org> - 2012-02-27 22:42 +0000
                              Re: what is the right value about a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-27 18:54 -0500
              Re: what is the right value about a = a++; Barry Schwarz <schwarzb@dqel.com> - 2012-02-07 09:26 -0800
            Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-07 15:12 -0500
              Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-07 21:50 +0000
                Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-07 16:51 -0600
                  Re: what is the right value about a = a++; Robert Wessel <robertwessel2@yahoo.com> - 2012-02-07 22:49 -0600
                    Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-08 14:08 -0600
                      Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 00:18 +0000
                        Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-09 13:06 -0500
                          Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-09 13:58 -0600
                          Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 23:52 +0000
                          Re: what is the right value about a = a++; jgk@panix.com (Joe keane) - 2012-02-10 21:45 +0000
                            Re: what is the right value about a = a++; Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-22 00:09 +0200
                      Re: what is the right value about a = a++; Robert Wessel <robertwessel2@yahoo.com> - 2012-02-08 22:35 -0600
                    Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-08 15:42 -0500
                  Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-08 14:47 +0000
                    Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-08 14:29 -0600
                      Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 02:27 +0000
                        Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-09 23:10 -0500
                          Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 09:11 +0000
                            Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 07:26 -0500
                              Re: what is the right value about a = a++; Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-10 13:12 +0000
                                Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 11:07 -0500
                              Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 16:27 +0000
                                Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 12:17 -0500
                                  Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 12:09 -0600
                                    Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 18:17 -0500
                                      Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-11 10:15 +0000
                                        Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-13 16:04 -0500
                        Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 12:30 -0600
                    Re: what is the right value about a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-09 21:30 -0500
                      Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 02:48 +0000
                        Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-10 09:49 +0000
                  Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 00:15 +0000
                    Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-09 12:49 -0500
                      Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 23:51 +0000
                Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-08 15:39 -0500
                  Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 01:05 +0000
                    Re: what is the right value about a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-09 23:24 -0500
                      Re: what is the right value about a = a++; Jens Gustedt <jens.gustedt@loria.fr> - 2012-02-10 16:58 +0100
                        Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-10 23:32 +0000
                  Re: what is the right value about a = a++; John Bode <jfbode1029@gmail.com> - 2012-02-08 17:41 -0800
                    Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-08 22:25 -0500
                    Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-09 13:15 -0500
                      Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 23:57 +0000
                        Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 11:15 -0500
                          Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 11:58 -0600
                            Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 18:14 -0500
                          Re: what is the right value about a = a++; Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-10 22:43 +0000
                        Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 11:53 -0600
                        Re: what is the right value about a = a++; Joshua Maurice <joshuamaurice@gmail.com> - 2012-02-10 15:19 -0800
                  Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 02:32 +0000
                    Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-09 23:13 -0500
                      Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 09:34 +0000
                        Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 07:32 -0500
                          Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-10 22:24 +0000
                        Re: what is the right value about a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 11:35 -0500
                          Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 12:23 -0500
                Re: what is the right value about a = a++; falk@rahul.net (Edward A. Falk) - 2012-02-08 20:40 +0000
                  Re: what is the right value about a = a++; Keith Thompson <kst-u@mib.org> - 2012-02-08 15:00 -0800
                  Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 01:10 +0000
                    Re: what is the right value about a = a++; 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-09 20:43 -0800
                      Re: what is the right value about a = a++; Robert Wessel <robertwessel2@yahoo.com> - 2012-02-09 23:36 -0600
                  Re: what is the right value about a = a++; cri@tiac.net (Richard Harter) - 2012-02-10 02:40 +0000
                    Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-09 23:17 -0500
                Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-08 23:49 +0000
              Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-08 23:42 +0000
            Re: what is the right value about a = a++; blp@cs.stanford.edu (Ben Pfaff) - 2012-02-07 12:09 -0800
        Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-07 07:42 -0500
          Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-07 17:21 +0000
            Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-07 15:24 -0600
              Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 01:27 +0000
                Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 21:57 -0600
        Re: what is the right value about a = a++; "Joel C. Salomon" <joelcsalomon@gmail.com> - 2012-02-08 01:00 -0500
        Re: what is the right value about a = a++; falk@rahul.net (Edward A. Falk) - 2012-02-08 20:33 +0000
          Re: what is the right value about a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 02:05 +0000
      Re: what is the right value about  a = a++; Markus Wichmann <nullplan@gmx.net> - 2012-02-09 09:28 +0100
        Re: what is the right value about  a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 10:21 +0000
          Re: what is the right value about  a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-09 11:38 +0000
          Re: what is the right value about  a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-10 00:03 -0500
          Re: what is the right value about  a = a++; Philip Lantz <prl@canterey.us> - 2012-02-10 01:15 -0800
        Re: what is the right value about  a = a++; Kenneth Brody <kenbrody@spamcop.net> - 2012-02-09 13:22 -0500
    Re: what is the right value about a = a++; Slash QB <slashqb@gmail.com> - 2012-02-06 02:35 -0800
      Re: what is the right value about a = a++; Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-07 03:07 -0800
        Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-07 15:19 -0600
          Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-07 17:00 -0500
            Re: what is the right value about a = a++; Stephen Sprunk <stephen@sprunk.org> - 2012-02-10 12:05 -0600
              Re: what is the right value about a = a++; James Kuyper <jameskuyper@verizon.net> - 2012-02-10 13:46 -0500
    Re: what is the right value about  a = a++; Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-02-06 22:37 -0700
      Re: what is the right value about  a = a++; Kaz Kylheku <kaz@kylheku.com> - 2012-02-07 07:22 +0000
    Re: what is the right value about  a = a++; Shao Miller <sha0.miller@gmail.com> - 2012-02-07 01:18 -0500

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#16978

FromKaz Kylheku <kaz@kylheku.com>
Date2012-02-09 10:21 +0000
Message-ID<20120209015413.706@kylheku.com>
In reply to#16968
On 2012-02-09, Markus Wichmann <nullplan@gmx.net> wrote:
> On 06.02.2012 10:45, 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.
>
> On a related note, what about the following:
>
> static int foo;
> static int bar(void)
> {
>     return foo++;
> }
>
> int main(void)
> {
>     return foo++ - bar();
> }

I would have to say that this is unspecified behavior, not undefined.
There are these possibilities.

Scenario 1:

The processing of the foo++ in main takes place like this. First foo
has to be accessed, and then the new value stored. So the call to bar can occur
in three abstract places: 1) before foo is accessed, 2) after it is accessed but
before the new value is stored, or 3) after the new value is stored.

Case 1 is similar to this:

  int temp = bar();
  return foo++ - temp; /* foo becomes 2, 1 returned */

Case 2 is similar to this:

  int temp0 = foo, temp1 = bar(); /* 0, 0 */
  return foo = temp0 + 1, temp0; /* foo becomes 1, 0 returned */

Case 3:

  int temp = foo++; /* 0, foo becomes 1 */
  return temp - bar(); /* foo becomes 2, -1 returned */

Notice the symmetry between case 1 and 3.  2 is the most complex; we need
to expose its semantics using two temporary values. This is because the call to
bar() is interposed into the evaluation of foo++.

Scenario 2:

foo++ works by accessing the value, adding 1 to it, storing the new value,
accessing the value again and then subtracting 1 to produce the resulting
value. If foo is not volatile, this is allowed because the accesses are not
visible behavior.  Now bar() can be called first, after the value is accessed,
after it is stored, then after it is accessed again. Here you have four cases
that can be worked out similarly to Scenario 1.

[toc] | [prev] | [next] | [standalone]


#16979

FromKaz Kylheku <kaz@kylheku.com>
Date2012-02-09 11:38 +0000
Message-ID<20120209032147.759@kylheku.com>
In reply to#16978
On 2012-02-09, Kaz Kylheku <kaz@kylheku.com> wrote:
> I would have to say that this is unspecified behavior, not undefined.
> There are these possibilities.

By the way I did consider the possibility that the access to foo may not be
atomic. I don't think that a function call can be issued in the middle of an
access to a global, because it seems that the sequence point isn't honored in
that case, but I will chew on that one.

The main thing about this sequence point crap is that you can look smart while
discussing it, even if the conclusions are vague.

[toc] | [prev] | [next] | [standalone]


#17035

FromShao Miller <sha0.miller@gmail.com>
Date2012-02-10 00:03 -0500
Message-ID<jh289g$ed2$1@dont-email.me>
In reply to#16978
On 2/9/2012 05:21, Kaz Kylheku wrote:
> On 2012-02-09, Markus Wichmann<nullplan@gmx.net>  wrote:
>> On 06.02.2012 10:45, 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.
>>
>> On a related note, what about the following:
>>
>> static int foo;
>> static int bar(void)
>> {
>>      return foo++;
>> }
>>
>> int main(void)
>> {
>>      return foo++ - bar();
>> }
>
> I would have to say that this is unspecified behavior, not undefined.
> There are these possibilities.
>

In C11, the evaluation of 'foo++' is indeterminately sequenced relative 
to the call to 'bar'.  It is either before or after, but not a portion 
before and a portion after.[5.1.2.3p3, 6.5.2.2p10]

Then the behaviour doesn't appear to fall into 6.5p2, since it is no 
longer unsequenced given the consideration above.  So yes, it looks to 
be unspecified, in C11.

[toc] | [prev] | [next] | [standalone]


#17044

FromPhilip Lantz <prl@canterey.us>
Date2012-02-10 01:15 -0800
Message-ID<MPG.299e800d1291588898968b@news.eternal-september.org>
In reply to#16978
Kaz Kylheku wrote:
> On 2012-02-09, Markus Wichmann wrote:
> > On a related note, what about the following:
> >
> > static int foo;
> > static int bar(void)
> > {
> >     return foo++;
> > }
> >
> > int main(void)
> > {
> >     return foo++ - bar();
> > }
> 
> I would have to say that this is unspecified behavior, not undefined.
> There are these possibilities.
> 
> Scenario 1:
> 
> The processing of the foo++ in main takes place like this. First foo
> has to be accessed, and then the new value stored. So the call to bar can occur
> in three abstract places: 1) before foo is accessed, 2) after it is accessed but
> before the new value is stored, or 3) after the new value is stored.
> 
> Case 1 is similar to this:
> 
>   int temp = bar();
>   return foo++ - temp; /* foo becomes 2, 1 returned */
> 
> Case 2 is similar to this:
> 
>   int temp0 = foo, temp1 = bar(); /* 0, 0 */
>   return foo = temp0 + 1, temp0; /* foo becomes 1, 0 returned */
> 
> Case 3:
> 
>   int temp = foo++; /* 0, foo becomes 1 */
>   return temp - bar(); /* foo becomes 2, -1 returned */
> 
> Notice the symmetry between case 1 and 3.  2 is the most complex; we need
> to expose its semantics using two temporary values. This is because the call to
> bar() is interposed into the evaluation of foo++.

I don't think your case 2 is allowed. If foo++ is evaluated before the 
call to bar(), then the sequence point before the call ensures that the 
side effect is complete before the call.

[toc] | [prev] | [next] | [standalone]


#16990

FromKenneth Brody <kenbrody@spamcop.net>
Date2012-02-09 13:22 -0500
Message-ID<gMednbNry-lgk6nSnZ2dnUVZ_qadnZ2d@bestweb.net>
In reply to#16968
On 2/9/2012 3:28 AM, Markus Wichmann wrote:
[...]
> On a related note, what about the following:
>
> static int foo;
> static int bar(void)
> {
>      return foo++;
> }
>
> int main(void)
> {
>      return foo++ - bar();
> }
>
> The standard sticks a sequence point between the evaluation of a
> function call's arguments but not after its return.

But there _is_ a sequence point as part of the return statement itself.

> bar() is not
> guarenteed to be called before or after evaluation of foo++, so bar()
> can have different results. So does the above program exhibit UB?

I believe it's unspecified, due to the unspecified nature of order of 
evaluation.  There is guaranteed to be a sequence between the two 
increments, regardless of evaluation order.

> Because if it does, that means that no-one should use function calls in
> compound expressions. Unless the function called is pure or there's only
> one function call which is compounded only with local variables (i.e.
> variables with a scope smaller than file scope).


-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


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

FromSlash QB <slashqb@gmail.com>
Date2012-02-06 02:35 -0800
SubjectRe: what is the right value about a = a++;
Message-ID<f0278160-eb52-43a9-860f-385c97f0d40d@vh10g2000pbc.googlegroups.com>
In reply to#16806
> 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.

[toc] | [prev] | [next] | [standalone]


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

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2012-02-07 03:07 -0800
SubjectRe: what is the right value about a = a++;
Message-ID<b2a9ccca-c5a8-43ed-a240-244a5dae089a@s7g2000vby.googlegroups.com>
In reply to#16810
On Feb 6, 10:35 am, Slash QB <slas...@gmail.com> 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)

[toc] | [prev] | [next] | [standalone]


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

FromStephen Sprunk <stephen@sprunk.org>
Date2012-02-07 15:19 -0600
SubjectRe: what is the right value about a = a++;
Message-ID<jgs4gi$6ad$1@dont-email.me>
In reply to#16832
On 07-Feb-12 05:07, Nick Keighley wrote:
> On Feb 6, 10:35 am, Slash QB <slas...@gmail.com> 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)

Compilers existed prior to the standard, so their divergent behavior
could not possibly be blamed on a non-existent standard allowing it.

AIUI, the job of the ANSI committee was to document the language as it
existed, warts and all, rather than to create a new and perfect language
from scratch.  They did overstep that boundary on occasion, eg.
prototypes and (void *), but did so in ways that didn't break any
existing code.

As it turned out, different compilers treated "a=a++;" differently, and
the committee (rightly, IMHO) decided to allow them to continue with
that rather than break existing code that depended on a particular
compiler's ex post facto outlawed behavior.

While I've never taken an exhaustive inventory, I suspect most undefined
behavior in the standard could be traced to such a scenario.

S

-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


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

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-02-07 17:00 -0500
SubjectRe: what is the right value about a = a++;
Message-ID<4F319F0D.1000809@verizon.net>
In reply to#16864
On 02/07/2012 04:19 PM, Stephen Sprunk wrote:
> On 07-Feb-12 05:07, Nick Keighley wrote:
>> On Feb 6, 10:35 am, Slash QB <slas...@gmail.com> 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)
> 
> Compilers existed prior to the standard, so their divergent behavior
> could not possibly be blamed on a non-existent standard allowing it.

True; but the fact that different current compilers give different
results cannot be used to validly infer that the behavior is undefined.
The behavior could be implementation-defined, or simply unspecified, or
one of the compilers you're comparing could be non-conforming.

> AIUI, the job of the ANSI committee was to document the language as it
> existed, warts and all, rather than to create a new and perfect language
> from scratch.

It was neither of those things, and both. The committee was tasked with
creating a language specification which was supposed to honor many
different competing principles; some of the hardest work the committee
had to do was balancing those different principles. Some of those
principles, if taken in isolation, could have been interpreted as
calling for a "perfect" language. Other principles called for retaining
compatibility with existing practice.

Incidentally, it was made explicitly clear that "existing code is
important; existing implementations are not". I would presume that was
based upon the fact that, for any reasonably successful implementation
of C, the amount of code written for use on that implementation greatly
outweighed the amount of code required to implement it.

That principle became less important when existing practice was in
conflict with itself. The committee could have specified one particular
choice of behavior, rendering all implementations that didn't make that
choice non-conforming. They could have left the behavior unspecified,
with the range of permitted options wide enough to cover the variety of
existing practice. They could have declared that the behavior was
implementation-defined, requiring a conforming implementation to
document what behavior it chose. Or they could have declared the
behavior undefined, which by definition is compatible with anything that
any compiler might choose to do with the code. The committee did all
four of those things, in different contexts.

[toc] | [prev] | [next] | [standalone]


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

FromStephen Sprunk <stephen@sprunk.org>
Date2012-02-10 12:05 -0600
SubjectRe: what is the right value about a = a++;
Message-ID<jh3m95$oho$1@dont-email.me>
In reply to#16871
On 07-Feb-12 16:00, James Kuyper wrote:
> On 02/07/2012 04:19 PM, Stephen Sprunk wrote:
>> On 07-Feb-12 05:07, Nick Keighley wrote:
>>>     the statement is undefined
>>>     =>
>>>     compilers are permitted to produce different answers
>>>    (or trap or crash or anything else weird the compiler writer feels
>>> like)
>>
>> Compilers existed prior to the standard, so their divergent behavior
>> could not possibly be blamed on a non-existent standard allowing it.
> 
> True; but the fact that different current compilers give different
> results cannot be used to validly infer that the behavior is undefined.
> The behavior could be implementation-defined, or simply unspecified, or
> one of the compilers you're comparing could be non-conforming.

True, divergent behavior could have been classified several different
ways by the Standard.  I was merely pointing out that divergent behavior
caused such classification, rather than the classification causing such
divergent behavior as you seemed to be saying.

> Incidentally, it was made explicitly clear that "existing code is
> important; existing implementations are not". I would presume that was
> based upon the fact that, for any reasonably successful implementation
> of C, the amount of code written for use on that implementation greatly
> outweighed the amount of code required to implement it.

True.  However, the importance of that statement comes from the fact
that a significant amount of "existing code" had come to _depend_ on the
behavior of a particular implementation.  Therefore, such behavior would
need to remain valid for that implementation.  The insides of a compiler
might need to change, and new features could be tacked on, but it
couldn't be done in a way that forced existing code to stop working, no
matter how ugly the code might be.

S

-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


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

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-02-10 13:46 -0500
SubjectRe: what is the right value about a = a++;
Message-ID<4F35660A.40603@verizon.net>
In reply to#17067
On 02/10/2012 01:05 PM, Stephen Sprunk wrote:
> On 07-Feb-12 16:00, James Kuyper wrote:
...
>> True; but the fact that different current compilers give different
>> results cannot be used to validly infer that the behavior is undefined.
>> The behavior could be implementation-defined, or simply unspecified, or
>> one of the compilers you're comparing could be non-conforming.
> 
> True, divergent behavior could have been classified several different
> ways by the Standard.  I was merely pointing out that divergent behavior
> caused such classification, rather than the classification causing such
> divergent behavior as you seemed to be saying.

I think it was Nick who made the comments you're referring to, not me.
And a more accurate summary of what he said would be that the
classification allows the divergent behavior, rather than causing it.

The standard has had a major impact on compiler diversity. I know
because I remember what it was like before standard C was commonplace.
The committee didn't bless all existing variations in C compilers; some
variations were allowed by using one of those classifications, but most
were disallowed, and the ones that were disallowed rather quickly
disappeared. Unlike C99, C90 was adopted rather quickly.

[toc] | [prev] | [next] | [standalone]


#16818

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2012-02-06 22:37 -0700
Message-ID<1bd39r9pby.fsf@pfeifferfamily.net>
In reply to#16806
lujnan <lujnan@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#16824

FromKaz Kylheku <kaz@kylheku.com>
Date2012-02-07 07:22 +0000
Message-ID<20120207082051.808@kylheku.com>
In reply to#16818
On 2012-02-07, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> 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.

[toc] | [prev] | [next] | [standalone]


#16822

FromShao Miller <sha0.miller@gmail.com>
Date2012-02-07 01:18 -0500
Message-ID<jgqflh$u53$1@dont-email.me>
In reply to#16806
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.

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

Back to top | Article view | comp.lang.c


csiph-web