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


Groups > comp.lang.c > #65794

Re: Unspecified Behaviors in C90 - part 3

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Unspecified Behaviors in C90 - part 3
Date 2015-07-17 11:00 -0700
Organization None to speak of
Message-ID <lntwt2is02.fsf@kst-u.example.com> (permalink)
References (6 earlier) <45ba9b21-0f12-4662-b89a-9f38c63a9486@googlegroups.com> <mo7qaj$4pr$1@dont-email.me> <d4367854-d7f5-45c6-b69b-68add43b8734@googlegroups.com> <mo9l3t$ntj$2@dont-email.me> <55A929D2.70908@verizon.net>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
[...]
> When the C standard says that the behavior is unspecified, that's a very
> different concept. It is permitted for the behavior of the program to be
> detectably different, depending upon which choice the implementation
> made. If one of the permitted options would produce behavior that you
> don't want to happen, then you're supposed to re-write the code so that
> it's no longer a permitted option. Such a re-write is always possible.

A concrete example might help:

#include <stdio.h>

int g(void) { putchar('g'); return 0; }

int h(void) { putchar('h'); return 0; }

void f(int x, int y) { /* nothing */ } 

int main(void) {
    f(g(), h());
    putchar('\n');
    return 0;
}

This program may print either "gh" or "hg", followed by a newline.
It may not (assuming the putchar() calls succeed) do anything else.

Which output is produced is *unspecified*.  There is no implication
in the standard that either order is the default or preferred
order; either may occur, and implementations are not required to
document whcih choice they make, or to make the same choice in all
circumstances.  The program could legally print "gh" one time and
"hg" the next time (though that would be mildly surprising).

This is entirely deliberate.

There might be legitimate performance reasons a compiler might
prefer one order over the other -- not in this particular case,
since g() and h() are so similar, but in other cases where, for
example, register allocation might be easier with one order than
with the other.

The current language rules give substantial flexibility both to
compiler authors and to programmers.

As a programmer, if I need a particular order, I can modify the
program to guarantee it (this has been demonstrated already).
If I don't care about the order, I can let the compiler choose
based on performance, or code size, or other criteria.

If the order were not unspecified, I could still write code that
enforces a particular order, but I would no longer be able to write
code that says "I don't care about the evaluation order, generate
the best code you can".  And if it were implementation-defined,
code that depends on a particular implementation's documented
behavior would still not be portable to other implementations with
different documented behavior.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-09 11:21 -0700
  Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-09 14:54 -0400
    Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-09 21:38 +0100
  Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-13 21:22 +0000
    Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-14 08:21 -0700
      Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-14 08:48 -0700
        Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-14 09:33 -0700
          Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-14 18:08 +0100
            Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-15 19:38 -0700
              Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 00:47 -0400
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:10 -0700
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 21:17 -0400
                Re: Unspecified Behaviors in C90 - part 3 maddoxr@acm.org - 2015-07-17 07:43 -0700
              Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-16 09:35 +0100
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:18 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:45 +0100
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 21:19 -0400
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-17 12:14 -0400
                Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-17 11:00 -0700
                Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-19 21:24 +0000
              Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-16 07:40 -0700
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:20 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:49 +0100
          Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-14 11:23 -0700
          Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-14 15:11 -0400
            Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-15 19:46 -0700
              Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 01:50 -0400
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:15 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:39 +0100
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 22:26 -0400
      Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-19 21:22 +0000
      Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-19 17:57 -0400

csiph-web