🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4517

Re: Statements, sequence points and execution order

#4517
From: Mike Rubenstein
Date: Sat, 30 Dec 1995 22:56
89 lines
3308 bytes
wiecek@ee.ualberta.ca (B. Wiecek) wrote:

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

Right.  But remember that the compiler can do anything it wants if
there's no way for a conforming program to detect that it is different
from what the standard dictates.

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

Only if a conforming program can't tell the difference.

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

As your program stands, it crashes because you are dereferencing a
null pointer.  That results in undefined behavior, so you can't count
on anything.  In principle, the compiler could reorder the calls.

However, there are conforming programs for which calling the function
after the printf() would result in different output (e.g., if it does
any output to stdout), and it would be illegal for the compiler to
change the order of the calls for such programs.  It's unlikely that a
compiler could tell the difference, so I doubt if the order of the
function calls would be changed.


Michael M Rubenstein
Technical Details
Message-ID: <30e5c15b.10973056@nntp.ix.netcom.com>
Path: arc1-ca.pugleaf.net!ix.netcom.com!netnews
References: <4c45oc$1sc8@pulp.ucs.ualberta.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!ix.netcom.com!netnews
From: miker3@ix.netcom.com (Mike Rubenstein)
Newsgroups: comp.std.c
Subject: Re: Statements, sequence points and execution order
Date: Sat, 30 Dec 1995 22:56:16 GMT
Organization: Netcom
Message-ID: <30e5c15b.10973056@nntp.ix.netcom.com>
References: <4c45oc$1sc8@pulp.ucs.ualberta.ca>
NNTP-Posting-Host: ix-dc17-21.ix.netcom.com
X-NETCOM-Date: Sat Dec 30  2:56:15 PM PST 1995
X-Newsreader: Forte Agent .99c/16.141