<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.placenet.org/benoit/index.php/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Harvard Business School of Echec - cpp</title>
  <link>http://www.placenet.org/benoit/index.php/</link>
  <description></description>
  <language>fr</language>
  <pubDate>Sat, 09 Aug 2008 08:26:39 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Iterate &amp; Erase</title>
    <link>http://www.placenet.org/benoit/index.php/post/2007/04/22/Iterate-Erase</link>
    <guid isPermaLink="false">urn:md5:c457d512cc4c20934fcc26c18051f7b5</guid>
    <pubDate>Sun, 22 Apr 2007 00:51:00 +0200</pubDate>
    <dc:creator>Benoît Dejean</dc:creator>
        <category>bug</category><category>cpp</category><category>STL</category>    
    <description>    &lt;p&gt;I have discovered a nice bug in my code. I had some stuff about iterating a &lt;code&gt;std::list&amp;lt;&amp;gt;&lt;/code&gt; and removing some elements at the same time. Like the following :&lt;/p&gt;

&lt;pre&gt;
std::list&amp;lt;&amp;gt; l;

iterator it(l.begin());

while (it != l.end()) {
	if (...)
		it = l.erase(it);
	else
		++it;
}
&lt;/pre&gt;


&lt;p&gt;Then i moved from &lt;code&gt;std::list&amp;lt;&amp;gt;&lt;/code&gt; to &lt;code&gt;std::set&amp;lt;&amp;gt;&lt;/code&gt; (ordered set). The code didn't compile as is because &lt;code&gt;std::set&amp;lt;&amp;gt;::erase&lt;/code&gt; returns &lt;code&gt;void&lt;/code&gt;. The STL documentation says &lt;q&gt;Erasing an element from a set also does not invalidate any iterators...&lt;/q&gt;. OK then, no return value.&lt;/p&gt;

&lt;pre&gt;
std::set&amp;lt;&amp;gt; s;

iterator it(s.begin());

while (it != s.end()) {
	if (...)
		s.erase(it);
	++it;
}
&lt;/pre&gt;


&lt;p&gt;It worked nicely for a long time. But once i got a double free crash. In order to debug it, i added many print. For example &lt;code&gt;{1 2 3 5 6 7} - {3}&lt;/code&gt; shoud have printed &lt;code&gt;{1 2 5 6 7}&lt;/code&gt; but instead i got &lt;code&gt;{1 2 2 5 6 7}&lt;/code&gt; or &lt;code&gt;{1 5 6 7 2}&lt;/code&gt; !&lt;/p&gt;


&lt;p&gt;So i read again the documentation : &lt;q&gt;Erasing an element from a set also does not invalidate any iterators, &lt;ins&gt;except, of course, for iterators that actually point to the element that is being erased.&lt;/ins&gt;&lt;/q&gt;. Bon sang !&lt;/p&gt;


&lt;p&gt;The correct way to do it :&lt;/p&gt;

&lt;pre&gt;
std::set&amp;lt;&amp;gt; s;

iterator it(s.begin())

while (it != s.end()) {
	iterator next(it);
	++next;
	if (...)
		s.erase(it);
	it = next;
}
&lt;/pre&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>Type safety</title>
    <link>http://www.placenet.org/benoit/index.php/post/2006/12/16/Type-safety</link>
    <guid isPermaLink="false">urn:md5:e8281184e271078e4fff655ea3e203bf</guid>
    <pubDate>Sat, 16 Dec 2006 20:35:00 +0100</pubDate>
    <dc:creator>Benoît Dejean</dc:creator>
        <category>GNOME</category>
        <category>cpp</category><category>gnome</category><category>system-monitor</category>    
    <description>    &lt;p&gt;Converting system-monitor to &lt;code&gt;C++&lt;/code&gt; helped me to spot a lot of small errors. The most common is about &lt;code&gt;enum&lt;/code&gt;. For example, &lt;code&gt;gtk_table_attach&lt;/code&gt; has arguments of type &lt;code&gt;GtkAttachOptions&lt;/code&gt;. Old code used 0 which is not a valid &lt;code&gt;GtkAttachOptions&lt;/code&gt; but doesn't yield any warning about this, not even at runtime. Thanks to &lt;code&gt;g++&lt;/code&gt;, i've been able to fix these errors.&lt;/p&gt;


&lt;p&gt;Moreover, &lt;code&gt;C++&lt;/code&gt; makes me able to write more readable code. I'm trying to convert some C code to &lt;code&gt;C++&lt;/code&gt;, and i just can't understand what i meant.&lt;/p&gt;


&lt;p&gt;&lt;code&gt;pretty_table-&amp;gt;app_hash = g_hash_table_new (NULL, NULL);&lt;/code&gt;&lt;/p&gt;


&lt;p&gt;&lt;code&gt;pretty_table-&amp;gt;default_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);&lt;/code&gt;&lt;/p&gt;


&lt;p&gt;No type information. I had to read all the code to remember the purpose of these data. Ugly &lt;code&gt;GUINT_TO_POINTER&lt;/code&gt; inside &lt;img src=&quot;/benoit/themes/default/smilies/smile.png&quot; alt=&quot;:)&quot; class=&quot;smiley&quot; /&gt; Using &lt;code&gt;C++&lt;/code&gt;, i simply use &lt;code&gt;map&amp;lt;string, int&amp;gt;&lt;/code&gt; which gives me type information and type safety &lt;img src=&quot;/benoit/themes/default/smilies/smile.png&quot; alt=&quot;:)&quot; class=&quot;smiley&quot; /&gt;&lt;/p&gt;</description>
    
    
    
      </item>
    
  <item>
    <title>I've lost 5 years of work</title>
    <link>http://www.placenet.org/benoit/index.php/post/2006/12/14/Ive-lost-5-years-of-work</link>
    <guid isPermaLink="false">urn:md5:7876c80262e9fc490404ebf02f5390fe</guid>
    <pubDate>Thu, 14 Dec 2006 23:15:00 +0100</pubDate>
    <dc:creator>Benoît Dejean</dc:creator>
        <category>GNOME</category>
        <category>cpp</category><category>gnome</category><category>system-monitor</category>    
    <description>    &lt;p&gt;In the process of converting system-monitor to C++, i had to rename most of the files. I've lost complete history of the whole project &lt;img src=&quot;/benoit/themes/default/smilies/sad.png&quot; alt=&quot;:(&quot; class=&quot;smiley&quot; /&gt;&lt;/p&gt;


&lt;p&gt;We'll be soon in 2007 and we're still stuck with CVS mid-80 features. Yes, i'm only 3 years older than CVS. I'm ready to move to {git,monotone,mercurial} but now it's too late to avoid damage.&lt;/p&gt;</description>
    
    
    
      </item>
    
</channel>
</rss>