<?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: Demystifying the volatile keyword</title>
	<atom:link href="http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/feed" rel="self" type="application/rss+xml" />
	<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html</link>
	<description>Stephen Doyle&#039;s Ramblings on Software Engineering</description>
	<lastBuildDate>Sat, 30 Jan 2010 21:57:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Briefe Schreiben</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-11</link>
		<dc:creator>Briefe Schreiben</dc:creator>
		<pubDate>Tue, 25 Nov 2008 02:28:40 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-11</guid>
		<description>Hey! Your Post &quot;Demystifying the volatile keyword&quot; is very interesting for me. Unfortunately my written English is not so good so I write in German: Dir, meinem liebsten, geh</description>
		<content:encoded><![CDATA[<p>Hey! Your Post &#8220;Demystifying the volatile keyword&#8221; is very interesting for me. Unfortunately my written English is not so good so I write in German: Dir, meinem liebsten, geh</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Doyle</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-10</link>
		<dc:creator>Stephen Doyle</dc:creator>
		<pubDate>Mon, 24 Nov 2008 01:05:06 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-10</guid>
		<description>Hi Preiserh,

Please feel free to translate this post. I&#039;m glad that you found it useful.

Steve</description>
		<content:encoded><![CDATA[<p>Hi Preiserh,</p>
<p>Please feel free to translate this post. I&#8217;m glad that you found it useful.</p>
<p>Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: www.preiserhoehung.de</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-9</link>
		<dc:creator>www.preiserhoehung.de</dc:creator>
		<pubDate>Sun, 23 Nov 2008 16:20:13 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-9</guid>
		<description>Hi I like your post &quot;Demystifying the volatile keyword&quot; so well that I like to ask you whether I should translate and linking back. Please give me an answer. Your Preiserh</description>
		<content:encoded><![CDATA[<p>Hi I like your post &#8220;Demystifying the volatile keyword&#8221; so well that I like to ask you whether I should translate and linking back. Please give me an answer. Your Preiserh</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: seo blog</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-12</link>
		<dc:creator>seo blog</dc:creator>
		<pubDate>Thu, 16 Oct 2008 15:18:10 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-12</guid>
		<description>This is a very interesting article - I have added your blgo to my favourrites, I really like it, keep up the good work!</description>
		<content:encoded><![CDATA[<p>This is a very interesting article &#8211; I have added your blgo to my favourrites, I really like it, keep up the good work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Levicki</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-8</link>
		<dc:creator>Igor Levicki</dc:creator>
		<pubDate>Sun, 27 Jul 2008 18:59:24 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-8</guid>
		<description>&gt;&gt;Have you observed a compiler optimizing away such a variable and resulting in a crash?

Yes I did.

&gt;&gt;I’m tempted to write a code snippet and see what some standard compilers do in this scenario - have you already done this?

I tested it only with Intel C++ Compiler and it crashes so I submitted an issue with Intel Premier Support to get some clarification and so far I haven&#039;t got anything except the confirmation that they have reproduced it.

Let me save you some effort, here is the code snippet I used:

#include

#define ALIGN	__declspec(align(16))

void zealot(void)
{
ALIGN	volatile float wkr[4096]; // 16384 bytes
ALIGN	volatile float wki[4096]; // 16384 bytes
ALIGN	volatile float ak0[4096]; // 16384 bytes
ALIGN	volatile float ak1[4096]; // 16384 bytes

	__asm	{
		xor	eax, eax
		mov	edx, 16384
		lea	esi, wkr // reference wkr
		lea	edi, ak0 // reference ak0
		mov	dword ptr [1024 + edi + edx], eax // ak1[256] = 0
	}
}

int main(void)
{
	zealot();
	printf(&quot;I am not a zealot!\n&quot;);
	return 0;
}

Arrays wki and ak1 get optimized away because there is no explicit reference in the inline assembler block.

In my opinion the original C/C++ standard is too vague on the volatile keyword definition. They do say that the compiler should not optimize if it is not sure that the variable cannot be changed from the outside but in this case &quot;outside&quot; really should mean &quot;outside of the compiler generated code&quot;, not &quot;outside of the function&quot;.</description>
		<content:encoded><![CDATA[<p>&gt;&gt;Have you observed a compiler optimizing away such a variable and resulting in a crash?</p>
<p>Yes I did.</p>
<p>&gt;&gt;I’m tempted to write a code snippet and see what some standard compilers do in this scenario &#8211; have you already done this?</p>
<p>I tested it only with Intel C++ Compiler and it crashes so I submitted an issue with Intel Premier Support to get some clarification and so far I haven&#8217;t got anything except the confirmation that they have reproduced it.</p>
<p>Let me save you some effort, here is the code snippet I used:</p>
<p>#include</p>
<p>#define ALIGN	__declspec(align(16))</p>
<p>void zealot(void)<br />
{<br />
ALIGN	volatile float wkr[4096]; // 16384 bytes<br />
ALIGN	volatile float wki[4096]; // 16384 bytes<br />
ALIGN	volatile float ak0[4096]; // 16384 bytes<br />
ALIGN	volatile float ak1[4096]; // 16384 bytes</p>
<p>	__asm	{<br />
		xor	eax, eax<br />
		mov	edx, 16384<br />
		lea	esi, wkr // reference wkr<br />
		lea	edi, ak0 // reference ak0<br />
		mov	dword ptr [1024 + edi + edx], eax // ak1[256] = 0<br />
	}<br />
}</p>
<p>int main(void)<br />
{<br />
	zealot();<br />
	printf(&#8220;I am not a zealot!\n&#8221;);<br />
	return 0;<br />
}</p>
<p>Arrays wki and ak1 get optimized away because there is no explicit reference in the inline assembler block.</p>
<p>In my opinion the original C/C++ standard is too vague on the volatile keyword definition. They do say that the compiler should not optimize if it is not sure that the variable cannot be changed from the outside but in this case &#8220;outside&#8221; really should mean &#8220;outside of the compiler generated code&#8221;, not &#8220;outside of the function&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack of All Trades &#187; Blog Archives &#187; links for 2008-07-23</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-7</link>
		<dc:creator>Jack of All Trades &#187; Blog Archives &#187; links for 2008-07-23</dc:creator>
		<pubDate>Wed, 23 Jul 2008 04:38:58 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-7</guid>
		<description>[...] Demystifying the volatile keyword » Software Ramblings What is volatile keyword means and when to use it (tags: C++ Programming) [...]</description>
		<content:encoded><![CDATA[<p>[...] Demystifying the volatile keyword » Software Ramblings What is volatile keyword means and when to use it (tags: C++ Programming) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Doyle</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-6</link>
		<dc:creator>Stephen Doyle</dc:creator>
		<pubDate>Sun, 20 Jul 2008 00:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-6</guid>
		<description>Ah - I hadn&#039;t thought of indirectly accessing that from an inline assembler block. In this case I&#039;m not sure that the volatile keyword adds any value.

Have you observed a compiler optimizing away such a variable and resulting in a crash? I&#039;m tempted to write a code snippet and see what some standard compilers do in this scenario - have you already done this?</description>
		<content:encoded><![CDATA[<p>Ah &#8211; I hadn&#8217;t thought of indirectly accessing that from an inline assembler block. In this case I&#8217;m not sure that the volatile keyword adds any value.</p>
<p>Have you observed a compiler optimizing away such a variable and resulting in a crash? I&#8217;m tempted to write a code snippet and see what some standard compilers do in this scenario &#8211; have you already done this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Levicki</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-5</link>
		<dc:creator>Igor Levicki</dc:creator>
		<pubDate>Sat, 19 Jul 2008 23:55:40 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-5</guid>
		<description>Yes but that local variable could be indirectly accessed (and thus its value changed) from inline assembler block -- then what?

It is clear that in such case optimizing the variable away would lead to a program crash because of a stack overflow.

I just checked C++ Standard draft and it is pretty unclear on that. I believe that there should be a way to tell the compiler &quot;do not optimize away unreferenced local variables&quot;.

What do you think?</description>
		<content:encoded><![CDATA[<p>Yes but that local variable could be indirectly accessed (and thus its value changed) from inline assembler block &#8212; then what?</p>
<p>It is clear that in such case optimizing the variable away would lead to a program crash because of a stack overflow.</p>
<p>I just checked C++ Standard draft and it is pretty unclear on that. I believe that there should be a way to tell the compiler &#8220;do not optimize away unreferenced local variables&#8221;.</p>
<p>What do you think?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Doyle</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-4</link>
		<dc:creator>Stephen Doyle</dc:creator>
		<pubDate>Fri, 18 Jul 2008 21:02:12 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-4</guid>
		<description>Sorry, I don&#039;t know for certain. I would suspect though that it is allowed to optimize away the unreferenced local variable. Volatile really only protects against optimizations that may affect the value of the variable. Since the value of unreferenced variables is not an issue, then everything should be ok if it is optimized away.</description>
		<content:encoded><![CDATA[<p>Sorry, I don&#8217;t know for certain. I would suspect though that it is allowed to optimize away the unreferenced local variable. Volatile really only protects against optimizations that may affect the value of the variable. Since the value of unreferenced variables is not an issue, then everything should be ok if it is optimized away.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Levicki</title>
		<link>http://softwareramblings.com/2008/05/demystifying-the-volatile-keyword.html/comment-page-1#comment-3</link>
		<dc:creator>Igor Levicki</dc:creator>
		<pubDate>Fri, 18 Jul 2008 20:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://softwareramblings.com/?p=25#comment-3</guid>
		<description>Do you perhaps have any idea what the C++ standard says regarding whether the compiler is allowed to optimize away unreferenced local variable marked as volatile?</description>
		<content:encoded><![CDATA[<p>Do you perhaps have any idea what the C++ standard says regarding whether the compiler is allowed to optimize away unreferenced local variable marked as volatile?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
