🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4520

Re: Statements, sequence points and execution order

#4520
From: Henry Spencer
Date: Sat, 30 Dec 1995 23:01
39 lines
2258 bytes
In article <4c45oc$1sc8@pulp.ucs.ualberta.ca> wiecek@ee.ualberta.ca (B. Wiecek) writes:
>As far as I remeber (see above) the standard requires that statements
>are executed in the same order they are written...

More precisely, the *observable results* must be *as if* the statements
were executed in the order written.  (Still more precision is possible,
but that is the basic idea.)  "Observable results" means things like I/O
operations and assignments to variables declared `volatile', where the
standard defines the correct behavior; it doesn't cover areas where the
standard says "it's up to the implementation". 

>	aptr = NULL;
>	*aptr = 1;

You're assuming that the results of this will be predictable -- that it
will cause a core dump, or something of that ilk, when an attempt is made
to execute it, and that nothing else untoward will happen.  The standard
makes no such promise.  The result of that second assignment statement is
undefined behavior -- this program can do *anything*.  You can't make any
meaningful predictions about the behavior of this code; a compiler can do
whatever it wants, because it is entitled to assume that you never attempt
to access memory via a NULL pointer. 

If your program does not depend on input data or use `volatile', the
compiler is perfectly entitled to do a complete symbolic execution of the
program at compile time, determine what output it's going to print and
what exit status it's going to have, and rewrite the program to: 

	#include <stdio.h>
	#include <stdlib.h>
	int main(void) {
		printf(...the correct output...);
		exit(...the correct status...);
	}

Nothing in the standard forbids this.
-- 
Look, look, see Windows 95.  Buy, lemmings, buy!   |       Henry Spencer
Pay no attention to that cliff ahead...            |   henry@zoo.toronto.edu
Technical Details
Message-ID: <DKF99t.916%spenford@zoo.toronto.edu>
Path: arc1-ca.pugleaf.net!cs.utexas.edu!utnut!utzoo!spenford!henry
References: <4c45oc$1sc8@pulp.ucs.ualberta.ca>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!cs.utexas.edu!utnut!utzoo!spenford!henry
From: Henry Spencer <henry@zoo.toronto.edu>
Subject: Re: Statements, sequence points and execution order
References: <4c45oc$1sc8@pulp.ucs.ualberta.ca>
Sender: henry%spenford@zoo.toronto.edu (Henry Spencer)
Organization: SP Systems, Toronto
Date: Sat, 30 Dec 1995 23:01:04 GMT
Message-ID: <DKF99t.916%spenford@zoo.toronto.edu>