🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4515

Statements, sequence points and execution order

#4515
From: B. Wiecek
Date: Sat, 30 Dec 1995 20:00
68 lines
2481 bytes
I have a problem: my copy of standard mysteriously dissapeared!
(somebody borrowed it from me and never returned? If I catch ya...)

But that isn't what I want to post about:)

As far as I remeber (see above) the standard requires that statements
are executed in the same order they are written, thus if I write:

	a = 1;		/* statement1 */
	b = a + 1;	/* statement2 */
	c = 3;		/* statement3 */
	d = c + 1;	/* statement4 */

the standard requires that these statements shall be executed in this order,
right?

However, today's suprscalar processors (eg. Alpha) and compilers for them
may/will reorder the above to something like:

	a = 1;		/* statement1 */
	c = 3;		/* statement3 */
	b = a + 1;	/* statement2 */
	d = c + 1;	/* statement4 */

Does the standard allow this?
If not, then the compiler is "de facto" non-conforming (formally).
If yes, then what about the code below?

	a = 1;					/* statement1 */
	a_bugged_function_call();		/* statement2 */
	b = 4;					/* statement3 */
	c = a + b;				/* statement4 */
	printf("DEBUG: here, c = %d\n", c);	/* statement5 */
	fflush(stdout);				/* statement6 */

Assume that the 'a_bugged_function_call' is eg.

void a_bugged_function_call(void)
{
	int *aptr;

	aptr = NULL;
	*aptr = 1;
}

Now an aggresive reordering (compiler) may create code like:

	b = 4;					/* statement3 */
	a = 1;					/* statement1 */
	c = a + b;				/* statement4 */
	printf("DEBUG: here, c = %d\n", c);	/* statement5 */
	fflush(stdout);				/* statement6 */
	a_bugged_function_call();		/* statement2 */

and the 'printf' will be executed before the run time crash, rendering
the information useless.

(since I have problem :( ) What is the standard's standpoint on
reordering (non-dependend) statements? function calls?

Any light on this mess?

B. Wiecek
wiecek@ee.ualberta.ca
------------------------------------------------------------------
"Do not lend your "C Standard" nor your wife, because they tend
to never be returned" - (!)
------------------------------------------------------------------
Technical Details
Message-ID: <4c45oc$1sc8@pulp.ucs.ualberta.ca>
Path: arc1-ca.pugleaf.net!rover.ucs.ualberta.ca!news

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!rover.ucs.ualberta.ca!news
From: wiecek@ee.ualberta.ca (B. Wiecek)
Newsgroups: comp.std.c
Subject: Statements, sequence points and execution order
Date: 30 Dec 1995 20:00:12 GMT
Organization: UA
Message-ID: <4c45oc$1sc8@pulp.ucs.ualberta.ca>
NNTP-Posting-Host: vaclab.ee.ualberta.ca
Mime-Version: 1.0
Content-Type: Text/Plain; charset=US-ASCII
X-Newsreader: WinVN 0.99.7