<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Incredible C++ Snippet - How it works</title>
	<link>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/</link>
	<description>This is C++, we all have our segfaults.</description>
	<pubDate>Sun, 20 May 2012 12:32:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Azumanga</title>
		<link>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-79</link>
		<dc:creator>Azumanga</dc:creator>
		<pubDate>Wed, 03 Dec 2008 22:32:32 +0000</pubDate>
		<guid>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-79</guid>
		<description>That code most certainly is illegal according to the C++ Standard! Furthermore, optimisation can change the behaviour of this type of code.

Try something like (I hope this comes out OK)

#include 


int main(void)
{
  int x;
  int y[5];

  x = 0;
  y[5] = 1;

  std::cout &#60;&#60; x &#60;&#60; std::endl;
}

With no optimisation this prints 1, because y[5] does overwrite the memory of x. However, with optimisation turned on, it outputs 0, because the compiler "knows" that x = 0, and just turns the code into:

int main(void)
{ std::cout &#60;&#60; 0 &#60;&#60; std::endl; }</description>
		<content:encoded><![CDATA[<p>That code most certainly is illegal according to the C++ Standard! Furthermore, optimisation can change the behaviour of this type of code.</p>
<p>Try something like (I hope this comes out OK)</p>
<p>#include </p>
<p>int main(void)<br />
{<br />
  int x;<br />
  int y[5];</p>
<p>  x = 0;<br />
  y[5] = 1;</p>
<p>  std::cout &lt;&lt; x &lt;&lt; std::endl;<br />
}</p>
<p>With no optimisation this prints 1, because y[5] does overwrite the memory of x. However, with optimisation turned on, it outputs 0, because the compiler &#8220;knows&#8221; that x = 0, and just turns the code into:</p>
<p>int main(void)<br />
{ std::cout &lt;&lt; 0 &lt;&lt; std::endl; }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kai</title>
		<link>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-77</link>
		<dc:creator>Kai</dc:creator>
		<pubDate>Sun, 12 Oct 2008 21:21:00 +0000</pubDate>
		<guid>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-77</guid>
		<description>as far as I know on linux and windows stack is alloceated that way...</description>
		<content:encoded><![CDATA[<p>as far as I know on linux and windows stack is alloceated that way&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MAttd00d</title>
		<link>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-76</link>
		<dc:creator>MAttd00d</dc:creator>
		<pubDate>Sun, 12 Oct 2008 16:43:29 +0000</pubDate>
		<guid>http://bka-bonn.de/wordpress/index.php/2008/09/24/incredible-c-snippet-how-it-works/#comment-76</guid>
		<description>Won't this vary depending on how the stack is allocated? You are assuming the stack grows up and not down.</description>
		<content:encoded><![CDATA[<p>Won&#8217;t this vary depending on how the stack is allocated? You are assuming the stack grows up and not down.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

