<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Code review</title>
		<link>http://www.allegro.cc/forums/view/602869</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 21 Jan 2010 09:23:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello!<br />I am newbie C++ programmer.<br />Last time I wanted to learn object-oriented design,<br />so I tried to write easy C++ application with classes.<br />I wrote a Langton&#39;s Ant cellular automaton simulator,<br />you can find my code here:<br /><a href="http://www.allegro.cc/depot/LILACALILACAisLangonsAntCellular">http://www.allegro.cc/depot/LILACALILACAisLangonsAntCellular</a></p><p>If anyone have some free time and want to help me,<br />you can download sources and review my program.<br />Whole code is about 300 lines, with about 100 lines of comments<br />and some lines in .h file.<br />All comments will be very usefull and helpful.</p><p>Thanks for help,<br />Adam
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Mon, 18 Jan 2010 23:44:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m unable to even get it to compile on Mac.</p><p>Compiling with debugging, I get a crash within main.cpp, on the call to &quot;<span class="source-code">langton.start<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span>&quot;, it giving me the error:
</p><pre class="terminal scroll">(gdb) r
Starting program: /Users/onlinecop/programming/temp/allegro/602869/LILACA/lilaca 
Reading symbols for shared libraries ...............................................................................+ done
Reading symbols for shared libraries . done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xb0001a70
[Switching to process 3049 thread 0x3213]
0x00003ba7 in _mangled_main () at main.cpp:42
42              langton.start();
(gdb) kill
Kill the program being debugged? (y or n) y
Current language:  auto; currently c++
(gdb) q</pre><p>

(My line numberings are different because of some stuff I played around with to get it to work.)</p><p>Using Allegro 4.2.3.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Tue, 19 Jan 2010 02:15:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Same deal here <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p><span class="remote-thumbnail"><span class="json">{"name":"600310","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/685a603f1660624021e023b6dc4991df.jpg","w":887,"h":539,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/685a603f1660624021e023b6dc4991df"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/8/685a603f1660624021e023b6dc4991df-240.jpg" alt="600310" width="240" height="145" /></span></p><p>I suppose you were lucky enough to don&#39;t have a memory crash.</p><p>EDIT: Can anybody explain me who invented the &quot;this-&gt;&quot; crap? I&#39;ve seen this repeated a lot, and I don&#39;t find it any different than calling it without it. </p><p>EDIT2:</p><p>This is the culprit:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 40</span><span class="k1">void</span> Field::reset<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<div class="highlight"><span class="number"> 41</span>    <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> i<span class="k3">=</span><span class="n">1</span><span class="k2">;</span> i<span class="k3">&lt;</span><span class="k3">=</span><span class="n">800</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> </div><div class="highlight"><span class="number"> 42</span>        <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> z<span class="k3">=</span><span class="n">1</span><span class="k2">;</span> z<span class="k3">&lt;</span><span class="k3">=</span><span class="n">800</span><span class="k2">;</span> z<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> </div><span class="number"> 43</span>            this-&gt;set_fill<span class="k2">(</span><span class="k1">false</span>, i, z<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 44</span>        <span class="k2">}</span>
<span class="number"> 45</span>    <span class="k2">}</span>
<span class="number"> 46</span>    this-&gt;filled_cells<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
<span class="number"> 47</span><span class="k2">}</span>
</div></div><p>

it should be:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 40</span><span class="k1">void</span> Field::reset<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 41</span>    <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">&lt;</span><span class="n">800</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 42</span>        <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> z<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> z<span class="k3">&lt;</span><span class="n">800</span><span class="k2">;</span> z<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 43</span>            this-&gt;set_fill<span class="k2">(</span><span class="k1">false</span>, i, z<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 44</span>        <span class="k2">}</span>
<span class="number"> 45</span>    <span class="k2">}</span>
<span class="number"> 46</span>    this-&gt;filled_cells<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
<span class="number"> 47</span><span class="k2">}</span>
</div></div><p>

EDIT3:<br />I can see this error repeated everywhere(counting from 1 instead of 0) <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Remember, a C programmer always starts from 0:<br /><span class="remote-thumbnail"><span class="json">{"name":"castigo.gif","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/9\/a98f488ebef42572340874c0a5ba611f.gif","w":600,"h":197,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/9\/a98f488ebef42572340874c0a5ba611f"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/9/a98f488ebef42572340874c0a5ba611f-240.jpg" alt="castigo.gif" width="240" height="78" /></span></p><p><img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 02:25:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847744#target">dario ff</a> said:</div><div class="quote"><p>Can anybody explain me who invented the &quot;this-&gt;&quot; crap? I&#39;ve seen this repeated a lot, and I don&#39;t find it any different than calling it without it.</p></div></div><p>
It&#39;s intended to fully qualify the variable, which makes the code more clear and also avoids subtle bugs like <span class="source-code">C::aab</span> versus <span class="source-code"><span class="k2">:</span><span class="k2">:</span>aaa</span> (AKA, a global variable <span class="source-code">aaa</span>). It&#39;s easy to accidentally type the latter and the compiler won&#39;t complain if it&#39;s compatible with the context it&#39;s used in. If you want to be really sure, you can always go with <span class="source-code">this-&gt;C::aab</span>. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Tue, 19 Jan 2010 04:00:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847759#target">bamccaig</a> said:</div><div class="quote"><p>makes the code more clear</p></div></div><p>
To me it looks really messy. To each it&#39;s own I guess <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /></p><div class="quote_container"><div class="title">bamccaig said:</div><div class="quote"><p>avoids subtle bugs like C::aab versus ::aaa (AKA, a global variable aaa).</p></div></div><p>

You edited fast <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> I saw an x there. That&#39;s more reasonable.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 04:05:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The only arguments for &quot;this-&gt;&quot; that I can agree with are:<br />- When writing an example, showing that we&#39;re referring to a class member.<br />- When using an IDE with autocompletion, getting the IDE to provide you just the class members.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 19 Jan 2010 04:18:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is a kind of situation where this-&gt; is very convenient (A&lt;T&gt;::foo is non-dependent, hence the compiler needs to be explicitly told that it comes from the template parent) although there are other, more verbose ways:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt;
<span class="k1">struct</span> A
<span class="k2">{</span>
    <span class="k1">void</span> foo<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> T&gt;
<span class="k1">struct</span> B: <span class="k1">public</span> A<span class="k3">&lt;</span>T&gt;
<span class="k2">{</span>
    <span class="k1">void</span> bar<span class="k2">(</span><span class="k2">)</span>
    <span class="k2">{</span>
<div class="highlight">        this-&gt;foo<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> </div>
    <span class="k2">}</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Tue, 19 Jan 2010 04:29:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I thank you all for so fast replies!</p><p>I know that counting is starting from 0 (I programmed before in asm, php, some bash and perl),<br />but I had segfaults when it was 0 (I dont know why), <br />now is ok when I hanged i&lt;=800 to i&lt;800.<br />But thanks for advice.</p><p>I tried to compile my code under windows (on virtual machine),<br />after some time with troubles with compilation (windows is very strange system)<br />I run dbg and changed some of my code:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> Field::reset<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">&lt;</span><span class="n">800</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> <span class="c">// i=1 and i&lt;800 (from &lt;=800)</span>
        <span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> z<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> z<span class="k3">&lt;</span><span class="n">800</span><span class="k2">;</span> z<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> <span class="c">// same here</span>
            this-&gt;set_fill<span class="k2">(</span><span class="k1">false</span>, i, z<span class="k2">)</span><span class="k2">;</span>
        <span class="k2">}</span>
    <span class="k2">}</span>
    this-&gt;filled_cells<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
same for Field::draw_dots.</p><p>I also received segfault here (Info::draw_info):</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/textprintf_ex" target="_blank"><span class="a">textprintf_ex</span></a><span class="k2">(</span>buffer, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, screen-&gt;h-20, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span>,<span class="k3">-</span><span class="n">1</span>, <span class="s">"To leave press [ESC]"</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

So I changed &quot;screen-&gt;h-20&quot; to &quot;780&quot; (I am not planning changing resolution in future).</p><p>Last error is appearing here (at System::start()):</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>this-&gt;buffer, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">800</span>, <span class="n">800</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Update screen</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Program received signal SIGSEGV, Segmentation fault.<br />0x679cc350 in blit ()
</p></div></div><p>

I have no ideas why there is segfault.<br />Any ideas?</p><p>EDIT:<br />about &quot;this-&gt;&quot;,<br />so I should use it always, but only when needed?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Tue, 19 Jan 2010 04:37:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve made some modifications. One of the most important ones is the one marked with ***. <a href="http://www.allegro.cc/files/attachment/600312">Here</a>.</p><p>I&#39;ll explain:</p><ul><li><p>If you&#39;re going to use a constant value such as 800, you better define a Macro or a constant variable somewhere. I added in langton.h:</p></li></ul><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="k1">const</span> <span class="k1">int</span> FIELD_SIZE_W<span class="k3">=</span><span class="n">800</span><span class="k2">;</span>
<span class="number"> 2</span><span class="k1">const</span> <span class="k1">int</span> FIELD_SIZE_H<span class="k3">=</span><span class="n">800</span><span class="k2">;</span>
</div></div><p>

And anything related to the field used this constant. This way, in case you need to change the size of the field, you won&#39;t need to code everything again.</p><ul><li><p>step in the Info class wasn&#39;t initialized with a suitable value. I added an init() method for the Info class, and called it as soon as the application started. You were getting WEIRD values for current step.</p><br /></li><li><p>I added this in the System::init()</p></li></ul><p><span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// ***</span></span></p><p>You weren&#39;t giving to rand any value, so you always had the same results for your rand calls. I gave it a value related to the system&#39;s clock. As you probably know, computers can&#39;t handle random, it&#39;s just math that appears to give random values, so you&#39;ve got to give it a value. It&#39;s funny that someone based on this in a casino and won the electronic lottery by predicting the numbers with a computer program.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 05:08:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your corrections Dario_ff,<br />but I still have segfault in same place:</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847768#target">Adam Kazimierczak</a> said:</div><div class="quote"><p>
Program received signal SIGSEGV, Segmentation fault.<br />0x0040faa0 in blit ()
</p></div></div><p>


By the way, it&#39;s very interesting method you used in conditions in loops:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">!</span><span class="k3">=</span>FIELD_SIZE_W<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Tue, 19 Jan 2010 06:04:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847768#target">Adam Kazimierczak</a> said:</div><div class="quote"><p>
I know that counting is starting from 0 (I programmed before in asm, php, some bash and perl),<br />but I had segfaults when it was 0 (I dont know why), <br />now is ok when I hanged i&lt;=800 to i&lt;800.<br />But thanks for advice.
</p></div></div><p>

So write crappy sloppy code that for unknown reasons solves the segfault you shouldn&#39;t have been getting? That&#39;s just about the polar opposite of writing good code.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847788#target">Adam Kazimierczak</a> said:</div><div class="quote"><p><span class="source-code"><span class="k1">for</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">!</span><span class="k3">=</span>FIELD_SIZE_W<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></span></p></div></div><p>

Should be &lt;, not !=. != will happily let it go beyond FIELD_SIZE_W if somehow i gets incremented past the limit, say by an extra i++ in your loop body or something.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Tue, 19 Jan 2010 06:05:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;d imagine that within the processing pipeline, the advantage of using <span class="source-code"><span class="k3">!</span><span class="k3">=</span></span> is that it simply checks for equality then negates that result; much faster than the <span class="source-code"><span class="k3">&lt;</span></span>, which subtracts the one from the other and checks whether that sets the overflow/underflow bit, the values are still unequal, etc.</p><p>The disadvantage is that if you happen to &#39;hop&#39; over the value you were checking for, you&#39;ve got either <span class="source-code"><span class="n">2</span>^<span class="n">32</span></span> or <span class="source-code"><span class="n">2</span>^<span class="n">64</span></span> more loop iterations to go through before it overflows back to <span class="source-code"><span class="n">0</span></span> and starts again.  Of course by then, you&#39;ve either segfaulted your application or unplugged your computer for the length of time it&#39;s taken...<br /><img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Tue, 19 Jan 2010 09:12:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847789#target">BAF</a> said:</div><div class="quote"><p>Should be &lt;, not !=. != will happily let it go beyond FIELD_SIZE_W if somehow ii gets incremented past the limit, say by an extra i++ in your loop body or something.</p></div></div><p>
<b>NO</b>, it should be !=. I won&#39;t hog the CPU for &quot;safety&quot; when doing loops of 800X800 <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> And if you use an extra i++? Then check if the value &quot;hopped&quot; above the limit, and break.</p><p>Thanks to Online cop for the tech explanation. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 16:24:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847835#target">dario ff</a> said:</div><div class="quote"><p>NO, it should be !=. </p></div></div><p>No. It shouldn&#39;t. In the off chance the value goes over the right hand side, it will keep looping till it wraps around. it SHOULD be &lt;. So it will always break when i is equal to or greater. Instead of only when it equals the right hand side.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I won&#39;t hog the CPU for &quot;safety&quot; when doing loops of 800X800 <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p></div></div><p>That makes no sense.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 19 Jan 2010 16:27:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes it does. At least to me... As OnlineCop said, checking for equality is much faster than &lt;.</p><p>What off chance are you talking about?</p><p>You know what? Nevermind, I&#39;ve never had problems with !=. </p><p>EDIT: Seriously, why are we talking about off chance? I already said that if you put an extra i++ you check the value and break <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 16:41:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m dubious about the statement on &#39;&lt;&#39; being &quot;much slower than &#39;!=&#39; in the first place (anybody got real-world facts on assembler instructions JNE and JL ?)</p><p>And in any case, I find it a bad idea(tm) to teach a self-called &quot;newbie C++ programmer&quot; to alter his way of coding an expression. Not before he has completely mastered structured programming; and counting &quot;from 0 included to MAX-1 included&quot; is really part of the basics.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 19 Jan 2010 17:23:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok ok, use &lt; instead of !=. IDK why I learned this way.</p><p>He says he didn&#39;t do the counting in the right way because he got segfault. Obviously, he screwed up somewhere else, and he was lucky that changing the value didn&#39;t crash at start. I&#39;m feeling generous, so I&#39;ll read the whole code to see what could be causing the segfault <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 17:38:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847838#target">dario ff</a> said:</div><div class="quote"><p>
As OnlineCop said, checking for equality is much faster than &lt;.
</p></div></div><p>
Not in any meaningful way. Maybe one or two clock ticks per iteration; on a 2GHz dual-core CPU, that&#39;s half a nanosecond. Less than the time a photon travels from the screen to your eye.</p><p>First of all, the loop overhead is negligible in 99% of all the loops you&#39;ll ever write. One or two extra clock ticks on a loop that takes thousands of clock ticks per iteration is literally nothing. If such a loop turns out to burn too much CPU time, see if you can get away with less iterations, with less calls to the loop itself, or with a better algorithm. Only if you are already using the best possible algorithm as sparsely as possible, and you are still having performance problems, consider optimizing code. But before doing so, give the compiler a fair chance of doing it for you (-O2), and when done, be sure to profile so you can be sure the code is really faster. You&#39;d be surprised.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
EDIT: Seriously, why are we talking about off chance? I already said that if you put an extra i++ you check the value and break
</p></div></div><p>
Consider the following code:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">!</span><span class="k3">=</span> <span class="n">10</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span>
  foobar<span class="k2">(</span>i<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Is this safe or not? You&#39;d be tempted to say &quot;yes&quot;, but what if foobar is defined like so:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> foobar<span class="k2">(</span><span class="k1">int</span><span class="k3">&amp;</span> i<span class="k2">)</span> <span class="k2">{</span>
  <span class="k3">+</span><span class="k3">+</span>i<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
It is not an off chance, it is a very realistic possible source of bugs that are extremely hard to find.<br />The only situation where I&#39;d use != over &lt; to iterate is with STL iterators: These often don&#39;t have a less-than comparison operator, and incrementing some_container::end() does nothing, so the iterator cannot possibly overrun (it can invalidate though, but that is a different question).</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847768#target">Adam Kazimierczak</a> said:</div><div class="quote"><p>
I know that counting is starting from 0 (I programmed before in asm, php, some bash and perl),<br />but I had segfaults when it was 0 (I dont know why), <br />now is ok when I hanged i&lt;=800 to i&lt;800.<br />But thanks for advice.
</p></div></div><p>
This approach is known as <a href="http://en.wikipedia.org/wiki/Shotgun_debugging">shotgun debugging</a> and generally considered bad practice.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Tue, 19 Jan 2010 19:12:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What if it is <span class="source-code"><span class="k1">void</span> foobar<span class="k2">(</span><span class="k1">int</span><span class="k3">&amp;</span> i<span class="k2">)</span></span> ? Then you should have used C instead of C++ !</p><p>And you would have got to use a <span class="source-code"><span class="k1">void</span> foobar<span class="k2">(</span> <span class="k1">int</span> <span class="k3">*</span>i<span class="k2">)</span></span> and a call on <span class="source-code">foobar<span class="k2">(</span> <span class="k3">&amp;</span>i <span class="k2">)</span>.</span></p><p>Which is way more talking than the C++ version who hide the fact that the function can possibly modify the values passed as its parameters.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Tue, 19 Jan 2010 19:34:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, I haven&#39;t looked at the whole code, but I made some other changes.</p><p>And because of the massive <span class="cuss"><span>bitch</span></span>ing around here, I replaced != with &lt; (but I think that just for pissing all of you I&#39;ll put !(i&gt;=FIELD_SIZE_W) <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p>I think the error you have in your VM is because it doesn&#39;t support your res. Remember the desktop res must be &gt;= to the window&#39;s res. Remember you have a square, 800x800, and it&#39;s possible your VM res is lower than that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Tue, 19 Jan 2010 20:41:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There seems to be lots of non-sense concerning &lt; vs != <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>A C++ programmer would be quite used to !=, since more often than not he uses iterators, which more often than not are not random access iterators and don&#39;t support less-than comparison in the first place.</p><p>Secondly, if the purpose of the loop is to go from 0 to N with no skipping and things, having it get stuck in an infinite or a very long loop (since the loop counter is unintentionally modified somewhere) is a good thing, since it helps you detect that something is wrong. If the loop just appeared to run normally, it might be harder to find out that the program is not running properly.</p><p>There seems to be a belief that code that makes efforts to cover up bugs is &quot;safe&quot; compared to code that rings a warning bell as soon as something is wrong.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Tue, 19 Jan 2010 21:38:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Audric,<br />I am newbie programmer in C++, but before I coded for many years<br />( I remember time when I was writing BASIC code at C64).<br />I used 1, because 0 caused segfaults on VM (on my GNU/Linux was work).</p><p>Dario_ff,<br />it&#39;s not VM fault.<br />I have VMWare with vmtools installed.<br />Default resolution is 1280x768 and... WAIT.<br />I completly forgot about it!<br />You have right, it&#39;s VM fault.<br />Thank you very much, now my program is working propertly!</p><p>Thank you all for helping me with segfaults and others!</p><p>Can we make a little summary?<br />I mean what faults I made in obiect-oriented model (with classes and others),<br />&quot;this-&gt;&quot; problem and others like that.<br />Here is final, working code:<br /><a href="http://www.allegro.cc/files/depot/2285/lilaca-1.0.tar.gz">http://www.allegro.cc/files/depot/2285/lilaca-1.0.tar.gz</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Tue, 19 Jan 2010 22:30:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847881#target">Adam Kazimierczak</a> said:</div><div class="quote"><p>&quot;this-&gt;&quot; problem and others like that.</p></div></div><p>

It&#39;s not a problem, it&#39;s just unnecessary.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Tue, 19 Jan 2010 23:31:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 42</span><span class="k1">int</span> i<span class="k2">;</span>
<span class="number"> 43</span><span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">!</span><span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="number"> 44</span><span class="k2">{</span>
<span class="number"> 45</span>  so_nyeah<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 46</span><span class="k2">}</span>
</div></div><p>

<b>EDIT</b>:</p><p>I had to change a few other things on my system so it&#39;d work.  Notably, you&#39;re still creating your screen dimensions at <span class="source-code"><span class="k2">(</span><span class="n">800</span>, <span class="n">800</span><span class="k2">)</span></span> and setting your ant&#39;s initial position to <span class="source-code"><span class="k2">(</span><span class="n">400</span>, <span class="n">400</span><span class="k2">)</span></span>.  But you&#39;ve defined your array to be <span class="source-code"><span class="n">800x600</span></span> through use of FIELD_SIZE_W and FIELD_SIZE_H.</p><p>So change within your langton.cpp the lines:
</p><div class="source-code snippet"><div class="inner"><pre>    <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED, <span class="n">800</span>, <span class="n">800</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
to
</p><div class="source-code snippet"><div class="inner"><pre>    <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED, FIELD_SIZE_W, FIELD_SIZE_H, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

and both the ant&#39;s position and the <span class="source-code">this-&gt;buffer</span> dimensions to the new screen dimensions:
</p><div class="source-code snippet"><div class="inner"><pre>    this-&gt;buffer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span><span class="n">800</span>, <span class="n">800</span><span class="k2">)</span><span class="k2">;</span>
    ...
    this-&gt;ant.set_position<span class="k2">(</span><span class="n">400</span>, <span class="n">400</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Center of view</span>
</pre></div></div><p>
to
</p><div class="source-code snippet"><div class="inner"><pre>    this-&gt;buffer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a>, <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span><span class="k2">;</span>
    ...
    this-&gt;ant.set_position<span class="k2">(</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a> <span class="k3">/</span> <span class="n">2</span>, <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a> <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Center of view</span>
</pre></div></div><p>

Then if you change your dimensions again, everything will be fine.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Tue, 19 Jan 2010 23:50:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Adam: Ah ok, sorry I didn&#39;t watch closely.
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847879#target">anonymous</a> said:</div><div class="quote"><p>There seems to be a belief that code that makes efforts to cover up bugs is &quot;safe&quot; compared to code that rings a warning bell as soon as something is wrong.</p></div></div><p>
More generally, there are two opposite extremes that are bad:<br />- The paper-thin programs that never care if what they do succeeds or not (and rely on it working); these programs crash if anybody looks at them sternly.<br />- The &quot;On error resume next&quot;, bulldozer programs. They recover from everything without even knowing what hit them.<br />In both case, it&#39;s because the programmer hasn&#39;t decided what to do if an unusual event happened.</p><p>I can cite 2 projects where user safety was more important than programmer convenience:<br />- a graphic editing program. Catches signals, re-allocates memory defensively, makes pre-emptive backups;  anything I can do to avoid data loss from the graphix man.<br />- a persistent MORPG server (it was multiplayer but not massive <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> ) You check &quot;world&quot; and pointers consistency everywhere, you stop executing a command or spell if it bugs out, and continue playing : Don&#39;t halt the server. It&#39;s harder for the coder to troubleshoot bugs, but players wants the world online 24/24, even if one item, command or spell happens to have no effect.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Wed, 20 Jan 2010 00:00:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Firstly you should check return values for allegro routines, particularly set_gfx_mode: for me this fails with the given 800x800 size and the program crashes later at the call to blit, since screen is NULL. (Works at 800x600.)</p><p>Next it is common to separate logic and drawing. The update methods generally should only deal with the logical state of the program, and the drawing should be handled by separate methods (this should help later when you want to start timing the program, and for example, when you can&#39;t draw things in the same order as you update them). That would also be in line with the OOP guideline that each method should do one task (updating logic and drawing are two separate tasks).</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">void</span> Ant::move<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  2</span>    <span class="k1">switch</span><span class="k2">(</span>this-&gt;direction<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  3</span>        <span class="k1">case</span> <span class="s">'N'</span><span class="k2">:</span>
<span class="number">  4</span>            this-&gt;set_position<span class="k2">(</span>this-&gt;pos_x,this-&gt;pos_y-1<span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number">  6</span>        <span class="k1">case</span> <span class="s">'E'</span><span class="k2">:</span>
<span class="number">  7</span>            this-&gt;set_position<span class="k2">(</span>this-&gt;pos_x<span class="k3">+</span><span class="n">1</span>,this-&gt;pos_y<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number">  9</span>        <span class="k1">case</span> <span class="s">'S'</span><span class="k2">:</span>
<span class="number"> 10</span>            this-&gt;set_position<span class="k2">(</span>this-&gt;pos_x,this-&gt;pos_y<span class="k3">+</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 12</span>        <span class="k1">case</span> <span class="s">'W'</span><span class="k2">:</span>
<span class="number"> 13</span>            this-&gt;set_position<span class="k2">(</span>this-&gt;pos_x-1,this-&gt;pos_y<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 15</span>    <span class="k2">}</span>
<span class="number"> 16</span><span class="k2">}</span>
<span class="number"> 17</span>
<span class="number"> 18</span><span class="k1">void</span> Ant::turn_left<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 19</span>    <span class="k1">switch</span><span class="k2">(</span>this-&gt;direction<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 20</span>        <span class="k1">case</span> <span class="s">'N'</span><span class="k2">:</span>
<span class="number"> 21</span>            this-&gt;set_direction<span class="k2">(</span><span class="s">'W'</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 23</span>        <span class="k1">case</span> <span class="s">'E'</span><span class="k2">:</span>
<span class="number"> 24</span>            this-&gt;set_direction<span class="k2">(</span><span class="s">'N'</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 26</span>        <span class="k1">case</span> <span class="s">'S'</span><span class="k2">:</span>
<span class="number"> 27</span>            this-&gt;set_direction<span class="k2">(</span><span class="s">'E'</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 29</span>        <span class="k1">case</span> <span class="s">'W'</span><span class="k2">:</span>
<span class="number"> 30</span>            this-&gt;set_direction<span class="k2">(</span><span class="s">'S'</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 31</span>            <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 32</span>    <span class="k2">}</span>
<span class="number"> 33</span><span class="k2">}</span>
</div></div><p>

I believe it greatly simplifies code to represent direction as a vector (increment along x and y axis). Then move would become:</p><div class="source-code snippet"><div class="inner"><pre>pos_x <span class="k3">+</span><span class="k3">=</span> increment_x<span class="k2">;</span>
pos_y <span class="k3">+</span><span class="k3">=</span> increment_y<span class="k2">;</span>
</pre></div></div><p>

and left and right turns would be (from memory, and using Python syntax, in C++ you&#39;d need a temporary variable):
</p><div class="source-code snippet"><div class="inner"><pre>increment_x, increment_y <span class="k3">=</span> <span class="k3">-</span>increment_y, increment_x #left
increment_x, increment_y <span class="k3">=</span> increment_y, <span class="k3">-</span>increment_x #right
</pre></div></div><p>

As to OOP, it is common not to give access to data members (make them private or protected) - although in a small and simple program such as this it might not matter much.</p><p>You might also make use of constructors, e.g instead of</p><div class="source-code snippet"><div class="inner"><pre>    this-&gt;ant.set_position<span class="k2">(</span><span class="n">400</span>,<span class="n">400</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Center of view</span>
    this-&gt;ant.set_direction<span class="k2">(</span><span class="s">'W'</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Becase first field is white, so turn left</span>
</pre></div></div><p>

... Ant might do that in the constructor and System might have a constructor that does:</p><div class="source-code snippet"><div class="inner"><pre>System::System<span class="k2">(</span><span class="k2">)</span><span class="k2">:</span>
   ant<span class="k2">(</span><span class="n">400</span>, <span class="n">400</span><span class="k2">)</span> <span class="c">//I suppose Ant could figure out the initial direction itself</span>
   <span class="c">//...</span>
<span class="k2">{</span>
   <span class="c">//...</span>
<span class="k2">}</span>
</pre></div></div><p>

The code also produces a couple of &quot;comparison between signed and unsigned expressions&quot; warnings. There are fixed by making these unsigned, as you&#39;d be comparing them against unsigned loop counters:</p><div class="source-code snippet"><div class="inner"><pre>    <span class="k1">const</span> <span class="k1">unsigned</span> FIELD_SIZE_W<span class="k3">=</span><span class="n">800</span><span class="k2">;</span>
    <span class="k1">const</span> <span class="k1">unsigned</span> FIELD_SIZE_H<span class="k3">=</span><span class="n">800</span><span class="k2">;</span>
</pre></div></div><p>

... and personally I&#39;d reserve ALL_CAPS only for preprocessor macros.</p><p>As to defensive programming, you might use assert for preconditions (e.g array indexes are not out of bounds) - this might have caught your earlier problems.</p><p>And using <b>this-&gt;</b> everywhere tends to be just noise.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847890#target">Audric</a> said:</div><div class="quote"><p>
I can cite 2 projects where user safety was more important than programmer convenience
</p></div></div><p>

Well, generally my point was that if you have foo <i>unintentionally</i> modifying the loop counter:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">!</span><span class="k3">=</span> N<span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span> <span class="k2">{</span>
   foo<span class="k2">(</span>i<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

... then changing != to &lt; neither addresses the problem not makes the program any safer. It is indeed rather a suggestion to use a &quot;on error resume next&quot; kind of safety net, which just happens to mask the real bug (you need to get at least simple, &quot;iterate from 0 to N&quot; -loops right, if you want any reliability from the program).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (anonymous)</author>
		<pubDate>Wed, 20 Jan 2010 00:04:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>anonymous</b>, I can see that as being a secondary constructor, but I would still keep the default no-parameter constructor, intact.  I&#39;ve been tweaking his code to use a <span class="source-code">std::vector<span class="k3">&lt;</span>Ant&gt;</span> instead of a single Ant instance.  It results in ants all over the place, and when they touch, their paths begin to interact.  Kinda pretty.</p><p>So if you want to add it for convenience, it&#39;s one thing.  But if you create a temporary instance of it that you&#39;ll push onto a <span class="source-code">vector</span> or linked <span class="source-code">list</span>, it becomes quite inconvenient.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Wed, 20 Jan 2010 02:08:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for this long and valuable advice, Anonymous!<br />I try to make those directions as a vector.<br />At first I think I will use constructors to set initial values (like ant position etc),<br />but I think, when I put all settings in one place (System::Init),<br />I will be able to change all settings of Field and Ant in one place.<br />Now I think I will add constructors as you said, but I will make them<br />params with all initial values, so I will be still able to change all values in one place.</p><p>OnlineCop, thank you too!<br />Your idea with std::vector is really great!<br />I thinked before that I will put Ant objects in array, but std::vector allows me<br />to add many Ant-s (without restrictions of array size).<br />In next releases I would to add option &quot;Add ant&quot;,<br />I am really interested in reactions between ants.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Wed, 20 Jan 2010 02:55:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You might want to learn STL.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Wed, 20 Jan 2010 03:43:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Attached zipped version contains updates to langton.(cpp|h) that incorporate vectors.</p><p>Note that if run, around timestep 10,000 you get a diagonal-right movement that continues until it hits the edge of the screen.</p><p>There, I found that it SEGFAULTS because you are trying to access memory outside of the array; there are no checks.  So within the move() function, I&#39;ve added a simple check to ensure that it doesn&#39;t do that.</p><p>It&#39;s not perfect, but more of a quick hack that I threw together to see what happens if the dimensions of the screen are really small (or if the program is allowed to run for an excessive amount of time).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Wed, 20 Jan 2010 04:06:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, CursedTyrant.<br />I have many thing things to learn.<br />Last time I thinked about to buy book about C++,<br />what you will recommend?<br />Whay do you think about &quot;Thinking in C++&quot;?</p><p>Anonymous, I forgot to talk about this:
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/847892#target">anonymous</a> said:</div><div class="quote"><p>
Next it is common to separate logic and drawing. The update methods generally should only deal with the logical state of the program, and the drawing should be handled by separate methods (this should help later when you want to start timing the program, and for example, when you can&#39;t draw things in the same order as you update them). That would also be in line with the OOP guideline that each method should do one task (updating logic and drawing are two separate tasks).
</p></div></div><p>
It looks like MVC (Model-View-Controller) model, wich I very often use in PHP.<br />Code is divided into 3 parts: program logic, output presentation and DB connection.<br />I dont know any programming models for desktop apps, can you recommend me any?</p><p>EDIT:<br />thank you OnlineCop!<br />I will take a look to the code in next few minutes</p><p>EDIT2:<br />Thank you again OnlineCop, for your time you spent in helping me writing that code!<br />I dont known that vectors and lists can be that very useful!<br />I will analyse that code and try write some improvements in my code in future
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Wed, 20 Jan 2010 04:10:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As far as &lt; vs !=, last I heard checking for equality does the same amount of work. Subtract one from the other. If that == 0, they are ==, otherwise !=. Using &lt; just checks that the result is less than zero.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Wed, 20 Jan 2010 04:45:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><ol><li><p>
-10, -1</p><p>is ([-10] &lt; [-1]) ?</p><p>compare: [-1] - [-10]<br />- Is negative bit set?<br />  YES: IS less-than</p><p>====</p><p>-1, -10</p><p>is ([-1] &lt; [-10]) ?</p><p>compare: [-10] - [-1]<br />- Is negative bit set?<br />  NO:<br />  - Is result == 0 ?<br />    NO: NOT less-than</p><p>====</p><p>-10, -10</p><p>is ([-10] &lt; [-10]) ?</p><p>compare: [-10] - [-10]<br />- Is negative bit set?<br />  NO:<br />  - Is result == 0 ?<br />    YES: NOT less-than
</p></li></ol><p>

Actually, the &quot;-1, -10&quot; and &quot;-10, -10&quot; can both short-circuit and return <span class="source-code"><span class="k1">false</span></span> as soon as it sees that the negative bit is NOT set.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Wed, 20 Jan 2010 08:52:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not quite sure what all that is supposed to mean, but there&#39;s one compare instruction, and a bunch of jumps that go with it. So in reality, you should end up with the same exact instruction count no matter which comparison you use.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Wed, 20 Jan 2010 09:56:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Compiling this with gcc -S:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">volatile</span> <span class="k1">int</span> x, y<span class="k2">;</span>
<span class="k1">int</span> main<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">while</span> <span class="k2">(</span>x <span class="k3">&lt;</span> y<span class="k2">)</span> <span class="k2">{</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"."</span><span class="k2">)</span><span class="k2">;</span><span class="k2">}</span>
    <span class="k1">while</span> <span class="k2">(</span>x <span class="k3">!</span><span class="k3">=</span> y<span class="k2">)</span> <span class="k2">{</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"."</span><span class="k2">)</span><span class="k2">;</span><span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

I get this for the two loop conditions:</p><div class="source-code snippet"><div class="inner"><pre>  cmpl  %eax, %edx
  jl  .L9
</pre></div></div><p>

and</p><div class="source-code snippet"><div class="inner"><pre>  cmpl  %eax, %edx
  jne  .L8
</pre></div></div><p>

The JL and JNE instructions take exactly the same time (which probably is one clock). So, no need to ever choose one over the other because of optimization reasons.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 21 Jan 2010 03:18:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I made a little test<br />My machine is 2.2GHz (2 core), using GNU/Linux 2.6, gcc 4.3.3<br />Here are results:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
adam@adam-pc ~ $ cat loop1.c <br />void main()                  <br />{                            <br />  unsigned int z, i;         <br />        for (z=0; z != 800; z++)<br />        {                       <br />                for (i = 0; i != 800; i++)<br />                {                         <br />                        printf(&quot;.&quot;);      <br />                }                         <br />        }                                 <br />}                                         <br />adam@adam-pc ~ $ cat loop2.c            <br />void main()                             <br />{                                       <br />  unsigned int z, i;                    <br />        for (z=0; z &lt; 800; z++)         <br />        {                               <br />                for (i = 0; i &lt; 800; i++)<br />                {                        <br />                        printf(&quot;.&quot;);     <br />                }                        <br />        }                                <br />}                                        <br />adam@adam-pc ~ $ gcc loop1.c -o loop1 &amp;&amp; chmod +x loop1<br />loop1.c: In function ‘main’:                           <br />loop1.c:8: warning: incompatible implicit declaration of built-in function ‘printf’<br />loop1.c:2: warning: return type of ‘main’ is not ‘int’                             <br />adam@adam-pc ~ $ gcc loop2.c -o loop2 &amp;&amp; chmod +x loop2<br />loop2.c: In function ‘main’:                           <br />loop2.c:8: warning: incompatible implicit declaration of built-in function ‘printf’<br />loop2.c:2: warning: return type of ‘main’ is not ‘int’                             
</p></div></div><p>

To generate times, i used:<br />$ time ./loop &gt;&gt; /dev/null</p><p>Chart:<br /><span class="remote-thumbnail"><span class="json">{"name":"resultsu.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/d\/8d60364aedc5ad61eb7fefb9059f082e.jpg","w":400,"h":585,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/d\/8d60364aedc5ad61eb7fefb9059f082e"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/8/d/8d60364aedc5ad61eb7fefb9059f082e-240.jpg" alt="resultsu.jpg" width="240" height="351" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Adam Kazimierczak)</author>
		<pubDate>Thu, 21 Jan 2010 03:44:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Uh oh, looks like &lt; is faster than != with those results. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Thu, 21 Jan 2010 06:54:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>#3&#39;s scary <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>3 vs 1, I guess &lt; wins.</p><p>Maybe new CPUs handle this better?<br />I must admit that I&#39;m also used a lot to != because of using STL a lot.</p><p>Your intended res was 800x600 originally? I thought it was odd that you used a square, but maybe you had a reason.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Thu, 21 Jan 2010 07:18:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t find the results significant when I see a printf(&quot;&quot;) in the middle of the loop, in my opinion the printf will take 95% of the execution time, even though it&#39;s actually redirected to /dev/null<br />If you want to profile a single assembler instruction, call it millions of time, not just 160000 times.<br />You final time counter has only 2 significant digits. It can&#39;t be much precise with so small number representation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 21 Jan 2010 07:42:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was being sarcastic. There is not enough data, and not enough deviation between the results to be conclusive at all. That and the fact that != isn&#39;t any faster or slower than &lt;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Thu, 21 Jan 2010 09:05:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I must read the whole post before I reply <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> I didn&#39;t look at the code. Now that I see printf, and not sure why he put that, the results are of no importance to me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dario ff)</author>
		<pubDate>Thu, 21 Jan 2010 09:12:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/602869/848039#target">Adam Kazimierczak</a> said:</div><div class="quote"><p>I made a little test</p></div></div><p>
Those results are the same within measurement error. If you want to do this properly, run the loop <i>much</i> more often than you do now, update a counter in the inner loop with something non-trivial (say, the loop variable) so that the loop actually does something (and something that the compiler can&#39;t predict and optimise away by static analysis) and then print the result at the end (otherwise the compiler will decide you&#39;re never using the thing you calculate and optimise it out).<br />Actually, scratch that, the proper way to do this is to do what Elias did.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>chmod +x loop1</p></div></div><p>
You don&#39;t need to do that. Doesn&#39;t hurt either, but it can be confusing to do things you don&#39;t need to.</p><p>Aaaaaanyway. Anyone who gets worried about whether &lt; or != is faster needs to get a girlfriend<span class="ref"><sup>[<a href="#">1</a>]</sup></span> because they obviously have <i>way</i> too much time on their hand.
</p><div class="ref-block"><h2>References</h2><ol><li>or boyfriend, if that is more applicable</li></ol></div></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 21 Jan 2010 09:19:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://en.wikipedia.org/wiki/Program_optimization#When_to_optimize">http://en.wikipedia.org/wiki/Program_optimization#When_to_optimize</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 21 Jan 2010 09:23:11 +0000</pubDate>
	</item>
</rss>
