<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Two postfix operations redux: sequence points</title>
	<atom:link href="http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/</link>
	<description>__author__ = &#039;Chris Leary&#039;</description>
	<lastBuildDate>Mon, 06 Sep 2010 22:38:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: cdleary</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1397</link>
		<dc:creator>cdleary</dc:creator>
		<pubDate>Fri, 15 Jan 2010 00:19:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1397</guid>
		<description>@Dan: Yeah, I don&#039;t see language lawyering as a good thing, it&#039;s just a funny concept that engineers will hang on every word of a spec, as I have just done. Nice examples of code that doesn&#039;t port! The intuition that program order is strictly top-to-bottom and left-to-right is certainly hard to overcome at times.</description>
		<content:encoded><![CDATA[<p>@Dan: Yeah, I don&#8217;t see language lawyering as a good thing, it&#8217;s just a funny concept that engineers will hang on every word of a spec, as I have just done. Nice examples of code that doesn&#8217;t port! The intuition that program order is strictly top-to-bottom and left-to-right is certainly hard to overcome at times.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Villiom Podlaski Christiansen</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1396</link>
		<dc:creator>Dan Villiom Podlaski Christiansen</dc:creator>
		<pubDate>Thu, 14 Jan 2010 09:44:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1396</guid>
		<description>I may have remembered wrong, considering the following results:

&lt;pre&gt;% for i in gcc clang; for a in i386 ppc; print -l &#039;&#039; &quot;$i ($a)&quot; &amp;&amp; $i -arch $a ./test.c &amp;&amp; ./a.out &amp;&amp; rm ./a.out

gcc (i386)
putint(2)
putint(1)
printvars(1, 2)

gcc (ppc)
putint(1)
putint(2)
printvars(1, 2)

clang (i386)
putint(1)
putint(2)
printvars(1, 2)

clang (ppc)
putint(1)
putint(2)
printvars(1, 2)&lt;/pre&gt;

As you can see, the evaluation order of function arguments of GCC varies depending on the architecture targeted. Rather than when trying XLC, it might have been when I tried to run the code on my Mac at home that I ran into this. Nonetheless, the fact that the evaluation order is changed by simple retargeting of the compiler underscores exactly what ‘unspecified’ and ‘undefined’ mean for portability…</description>
		<content:encoded><![CDATA[<p>I may have remembered wrong, considering the following results:</p>
<pre>% for i in gcc clang; for a in i386 ppc; print -l '' "$i ($a)" &amp;&amp; $i -arch $a ./test.c &amp;&amp; ./a.out &amp;&amp; rm ./a.out

gcc (i386)
putint(2)
putint(1)
printvars(1, 2)

gcc (ppc)
putint(1)
putint(2)
printvars(1, 2)

clang (i386)
putint(1)
putint(2)
printvars(1, 2)

clang (ppc)
putint(1)
putint(2)
printvars(1, 2)</pre>
<p>As you can see, the evaluation order of function arguments of GCC varies depending on the architecture targeted. Rather than when trying XLC, it might have been when I tried to run the code on my Mac at home that I ran into this. Nonetheless, the fact that the evaluation order is changed by simple retargeting of the compiler underscores exactly what ‘unspecified’ and ‘undefined’ mean for portability…</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Villiom Podlaski Christiansen</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1395</link>
		<dc:creator>Dan Villiom Podlaski Christiansen</dc:creator>
		<pubDate>Thu, 14 Jan 2010 09:33:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1395</guid>
		<description>Actually, a similar issue once bit me. What would you expect the code at the bottom of this post to print?

The results:

&lt;pre&gt;% clang ./test.c
% ./a.out
putint(1)
putint(2)
printvars(1, 2)
% gcc ./test.c
% ./a.out
putint(2)
putint(1)
printvars(1, 2)&lt;/pre&gt;

As part of a compiler course, I once wrote a VM in C, and — for brevity — I had function call sites as arguments of another function call. Later, I wanted to try — for fun — to compile the code with the IBM XLC compiler. The unexpected evaluation ordering function arguments caused the VM to crash. (This was in 2003, I believe.)

The lesson I learned from that is that you shouldn&#039;t make assumptions about when a C compiler decides to do what. Surprisingly large parts of the C language are unspecified, which in effect means implementation-defined; that is, you get what you, at the discretion of the compiler.

I never engaged much in language lawyering, for some reason, and it never really appealed to me. My best guess is that it&#039;s because I tend to distrust my own conclusions :-)

&lt;pre lang=&#039;c&#039;&gt;#include &lt;stdio.h&gt;

void printvars(int a, int b)
{
    printf(&quot;%s(%d, %d)\n&quot;, __func__, a, b);
}

int putint(int i)
{
    printf(&quot;%s(%d)\n&quot;, __func__, i);
    return i;
}

int main(int argc, const char *argv[])
{
    printvars(putint(1), putint(2));
}&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Actually, a similar issue once bit me. What would you expect the code at the bottom of this post to print?</p>
<p>The results:</p>
<pre>% clang ./test.c
% ./a.out
putint(1)
putint(2)
printvars(1, 2)
% gcc ./test.c
% ./a.out
putint(2)
putint(1)
printvars(1, 2)</pre>
<p>As part of a compiler course, I once wrote a VM in C, and — for brevity — I had function call sites as arguments of another function call. Later, I wanted to try — for fun — to compile the code with the IBM XLC compiler. The unexpected evaluation ordering function arguments caused the VM to crash. (This was in 2003, I believe.)</p>
<p>The lesson I learned from that is that you shouldn&#8217;t make assumptions about when a C compiler decides to do what. Surprisingly large parts of the C language are unspecified, which in effect means implementation-defined; that is, you get what you, at the discretion of the compiler.</p>
<p>I never engaged much in language lawyering, for some reason, and it never really appealed to me. My best guess is that it&#8217;s because I tend to distrust my own conclusions :-)</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> printvars<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s(%d, %d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> __func__<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> putint<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s(%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> __func__<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    printvars<span style="color: #009900;">&#40;</span>putint<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> putint<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: cdleary</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1394</link>
		<dc:creator>cdleary</dc:creator>
		<pubDate>Thu, 14 Jan 2010 02:39:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1394</guid>
		<description>@Dan: Your evidence has bested my finest lawyering. :-)

Apparently the subexpressions can be evaluated concurrently. I&#039;ll update the entry now to fix the mistake.</description>
		<content:encoded><![CDATA[<p>@Dan: Your evidence has bested my finest lawyering. :-)</p>
<p>Apparently the subexpressions can be evaluated concurrently. I&#8217;ll update the entry now to fix the mistake.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Villiom Podlaski Christiansen</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1393</link>
		<dc:creator>Dan Villiom Podlaski Christiansen</dc:creator>
		<pubDate>Mon, 11 Jan 2010 11:43:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1393</guid>
		<description>I believe ‘undefined’ and ‘unspecified’ mean exactly that; the result of the expression may vary depending on the compiler used, and possibly what it&#039;s targeting too.

For example, see this comparison of compiling your test program in Clang, Apple GCC &amp; LLVM GCC.

&lt;pre&gt;% clang -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
132
13
% gcc -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
test.c: In function ‘main’:
test.c:7: warning: operation on ‘z’ may be undefined
121
12
% llvm-gcc -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
test.c: In function ‘main’:
test.c:7: warning: operation on ‘z’ may be undefined
121
12&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I believe ‘undefined’ and ‘unspecified’ mean exactly that; the result of the expression may vary depending on the compiler used, and possibly what it&#8217;s targeting too.</p>
<p>For example, see this comparison of compiling your test program in Clang, Apple GCC &amp; LLVM GCC.</p>
<pre>% clang -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
132
13
% gcc -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
test.c: In function ‘main’:
test.c:7: warning: operation on ‘z’ may be undefined
121
12
% llvm-gcc -O3 -Wall -Wextra test.c&amp;&amp; ./a.out
test.c: In function ‘main’:
test.c:7: warning: operation on ‘z’ may be undefined
121
12</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: cdleary</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1392</link>
		<dc:creator>cdleary</dc:creator>
		<pubDate>Mon, 11 Jan 2010 01:12:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1392</guid>
		<description>@Michał: That&#039;s actually a very interesting question that I don&#039;t know the answer to (I see the same warning). Section 6.5 (Expressions) item 3 states, &quot;[T]he order of evaluation of subexpressions and the order in which side effects take place are both unspecified,&quot; which implies to me an that there &lt;em&gt;is&lt;/em&gt; a definitive ordering of subexpression evaluation, it&#039;s just unspecified which order it is. That could be a misinterpretation, though.

I guess we&#039;d have to read through GCC to figure out why it&#039;s giving that particular warning. This stuff is just too hokey to resolve from precise wording in the specifications. :-)</description>
		<content:encoded><![CDATA[<p>@Michał: That&#8217;s actually a very interesting question that I don&#8217;t know the answer to (I see the same warning). Section 6.5 (Expressions) item 3 states, &#8220;[T]he order of evaluation of subexpressions and the order in which side effects take place are both unspecified,&#8221; which implies to me an that there <em>is</em> a definitive ordering of subexpression evaluation, it&#8217;s just unspecified which order it is. That could be a misinterpretation, though.</p>
<p>I guess we&#8217;d have to read through GCC to figure out why it&#8217;s giving that particular warning. This stuff is just too hokey to resolve from precise wording in the specifications. :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michał Bartoszkiewicz</title>
		<link>http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/comment-page-1/#comment-1391</link>
		<dc:creator>Michał Bartoszkiewicz</dc:creator>
		<pubDate>Mon, 11 Jan 2010 00:54:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.cdleary.com/?p=709#comment-1391</guid>
		<description>Are you sure the C compiler isn&#039;t allowed to interpret identity(i++) * identity(i++) as (tmp1 = i, tmp2 = i, i = tmp1 + 1, i = tmp2 + 1, identity(tmp1) * identity(tmp2)) and print 121, 12?
gcc seems to warn on this code when invoked with -Wsequence-point:
&lt;pre&gt;x.c:7: warning: operation on &#039;i&#039; may be undefined&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Are you sure the C compiler isn&#8217;t allowed to interpret identity(i++) * identity(i++) as (tmp1 = i, tmp2 = i, i = tmp1 + 1, i = tmp2 + 1, identity(tmp1) * identity(tmp2)) and print 121, 12?<br />
gcc seems to warn on this code when invoked with -Wsequence-point:</p>
<pre>x.c:7: warning: operation on 'i' may be undefined</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
