Article View: comp.std.c
Article #4540Re: Statements, sequence points and execution order
From: Jerry Coffin
Date: Mon, 01 Jan 1996 03:09
Date: Mon, 01 Jan 1996 03:09
51 lines
2157 bytes
2157 bytes
wiecek@ee.ualberta.ca (B. Wiecek) wrote:
>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?
As long as a, b, c and d are all non-volatile, it can reorder as it
wishes, as long as there's no way of telling the difference. If they're
all non-volatile, the compiler is free to produce something like:
a=1;
b=2;
c=3;
d=4;
and execute them all simultaneously on a 4+ processor machine. Or, if
these are static, and haven't been previously used, it could treat it
all as initialized data, with no actual execution involved at all.
>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?
It all depends on whether any or all of a, b, c or d is volatile or not.
With volatile variables, the value of each variable must be correct at
each sequence point, and it's required to actually read/write the memory
location to obtain/store the value. E.g. even if the value of a is in a
register after setting a in statement 1, it's still required to read the
value from memory when it executes statement 2. Likewise with the value
of c in statement 3 & 4.
Later,
Jerry.
/* I can barely express my own opinions; I certainly can't
* express anybody else's.
*
* The universe is a figment of its own imagination.
*/
Technical Details
Message-ID:
<4c7ffq$p8j@natasha.rmii.com>Path:
arc1-ca.pugleaf.net!news.rmii.com!usenetReferences:
<4c45oc$1sc8@pulp.ucs.ualberta.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.rmii.com!usenet From: jcoffin@rmii.com (Jerry Coffin) Newsgroups: comp.std.c Subject: Re: Statements, sequence points and execution order Date: Mon, 01 Jan 1996 03:09:06 GMT Organization: TAEUS Message-ID: <4c7ffq$p8j@natasha.rmii.com> References: <4c45oc$1sc8@pulp.ucs.ualberta.ca> NNTP-Posting-Host: slip8158.rmii.com X-Newsreader: Forte Free Agent 1.0.82