<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8939389355392390587</id><updated>2011-06-22T00:28:22.471+01:00</updated><category term='garbage'/><category term='linux'/><category term='feed'/><category term='verification'/><category term='unit test'/><category term='java'/><category term='unix'/><category term='rss'/><category term='latex'/><category term='assertions'/><category term='mock'/><category term='performance'/><category term='testing'/><category term='blog'/><category term='c++'/><category term='pdf'/><category term='memory leak'/><title type='text'>Programming Languages hacks</title><subtitle type='html'>General important rules for programming languages like C, C++, Java</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8939389355392390587.post-288236496213109666</id><published>2006-12-07T15:50:00.000+01:00</published><updated>2008-12-09T02:07:19.389+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='performance'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='memory leak'/><category scheme='http://www.blogger.com/atom/ns#' term='garbage'/><title type='text'>Java Garbage Collection: How it works, How to control it</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_jbPuzDFEguM/RXg_Pe68J8I/AAAAAAAAABA/HCvHHcXBJW8/s1600-h/Java-Logo.png"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_jbPuzDFEguM/RXg_Pe68J8I/AAAAAAAAABA/HCvHHcXBJW8/s400/Java-Logo.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5005820521053169602" /&gt;&lt;/a&gt;&lt;br /&gt;Java has Garbage Collection. Good thing, yes! But, say with me:&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;The Garbage Collector deallocates all objects with zero references &lt;span style="font-weight:bold;"&gt;when it wants&lt;/span&gt;!!&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;Two troubles:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;Objects with zero references&lt;/span&gt;: typically a Java code looks like:&lt;br /&gt;&lt;pre&gt;&lt;CODE CLASS=Java&gt;public void doSomething() {&lt;br /&gt;    Configuration c = new Configuration("test.conf");&lt;br /&gt;    long limit = c.getLimit();    &lt;br /&gt;&lt;br /&gt;    for(long i = 0; i&amp;leq;limit ; i++) {&lt;br /&gt;       String s = new String(i);&lt;br /&gt;       System.out.println(s);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/CODE&gt;&lt;/pre&gt;&lt;br /&gt;The Configuration instance lives until the method's end. We can write:&lt;br /&gt;&lt;pre&gt;&lt;CODE CLASS=Java&gt;public void doSomething() {&lt;br /&gt;    Configuration c = new Configuration("test.conf");&lt;br /&gt;    long limit = c.getLimit();&lt;br /&gt;    c = null;     // good! now c has 0 reference!    &lt;br /&gt;&lt;br /&gt;    for(long i = 0; i&amp;leq;limit ; i++) {&lt;br /&gt;       String s = new String(i);&lt;br /&gt;       System.out.println(s);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/CODE&gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;GC deallocates them when it wants&lt;/span&gt;: it seems that the String instances will be cleaned when they go out of scope, but it isn't!!. &lt;br /&gt;The String instances are in a particular state called &lt;span style="font-weight:bold;"&gt;Invisible State&lt;/span&gt;. &lt;br /&gt;The GC cleans all the object that go out of scope &lt;span style="font-weight:bold;"&gt;only when the method doSomething() returns&lt;/span&gt;!!&lt;br /&gt;Simply, manually set the reference to null:&lt;br /&gt;&lt;pre&gt;&lt;CODE CLASS=Java&gt;public void doSomething() {&lt;br /&gt;    Configuration c = new Configuration("test.conf");&lt;br /&gt;    long limit = c.getLimit();&lt;br /&gt;    c = null;     // good! now c has 0 reference!    &lt;br /&gt;&lt;br /&gt;    for(long i = 0; i&amp;leq;limit ; i++) {&lt;br /&gt;       String s = new String(i);&lt;br /&gt;       System.out.println(s);&lt;br /&gt;       s = null;    // good! now s has 0 reference!&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/CODE&gt;&lt;/pre&gt;&lt;br /&gt;Now all our Object are &lt;span style="font-weight:bold;"&gt;only eligible for cleaning&lt;/span&gt;. &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;You cannot force garbage collection, just suggest it!&lt;/span&gt; You can suggest GC with &lt;code&gt;System.gc()&lt;/code&gt;, but this &lt;span style="font-weight:bold;"&gt;does not guarantee when&lt;/span&gt; it will happen.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Interesting links: &lt;a href="http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html"&gt;Sun's Truth About Garbage Collection&lt;/a&gt;,&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8939389355392390587-288236496213109666?l=programminghacks.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/288236496213109666/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8939389355392390587&amp;postID=288236496213109666' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/288236496213109666'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/288236496213109666'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/2006/12/java-garbage-collection-how-it-works.html' title='Java Garbage Collection: How it works, How to control it'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_jbPuzDFEguM/RXg_Pe68J8I/AAAAAAAAABA/HCvHHcXBJW8/s72-c/Java-Logo.png' height='72' width='72'/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8939389355392390587.post-629653026580872104</id><published>2006-11-27T11:58:00.000+01:00</published><updated>2008-12-09T02:07:19.481+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='blog'/><category scheme='http://www.blogger.com/atom/ns#' term='feed'/><category scheme='http://www.blogger.com/atom/ns#' term='rss'/><title type='text'>How to Optimize your RSS Feed and Promote your Blog or Site</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_jbPuzDFEguM/RXXzjabdUNI/AAAAAAAAAAM/vWuqroeM_wM/s1600-h/feed-icon-256x256.png"&gt;&lt;img style="float:right; margin:0 10px 10px 0;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_jbPuzDFEguM/RXXzjabdUNI/AAAAAAAAAAM/vWuqroeM_wM/s400/feed-icon-256x256.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5005174350607634642" /&gt;&lt;/a&gt;&lt;br /&gt;These are some &lt;span style="font-weight:bold;"&gt;optimizing&lt;/span&gt; rules:&lt;ol&gt;&lt;li&gt;The &lt;span style="font-weight:bold;"&gt;title&lt;/span&gt; should contain important search terms: the title should entice the reader to read on, not mislead them&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Add channel &lt;span style="font-weight:bold;"&gt;description&lt;/span&gt; field. It provides an opportunity to expand on the broad theme of the RSS feed&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The &lt;span style="font-weight:bold;"&gt;item titles&lt;/span&gt; should be 50-75 characters with spaces&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;Link text&lt;/span&gt; should emphasize keywords: be sure to use keywords in any link text that points back to your website&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Don't use Buttons like "Add to Google", "My Yahoo!" and don't use javascript or similar. Make &lt;span style="font-weight:bold;"&gt;subscribing standard, simpler and compatible&lt;/span&gt; with one button only&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Include the &lt;span style="font-weight:bold;"&gt;feed on a personal my.yahoo or my.msn&lt;/span&gt; home page: it's the fastest way to have an RSS feed spidered by Yahoo or MSN is to &lt;/li&gt;&lt;br /&gt;&lt;li&gt;Give your subscribers &lt;span style="font-weight:bold;"&gt;easy ways to email, tag, share, and act&lt;/span&gt; on the content you publish&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Share collected links (or photos) in your feed, too&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Geotag your feed: let everyone know your publication location by adding your latitude and longitude to your feed&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Add some color to your feed. Place a special image (like a corporate logo) in your feed so that it stands out from the pack when displayed in many popular RSS news readers&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;These are some &lt;span style="font-weight:bold;"&gt;promoting&lt;/span&gt; rules:&lt;ol&gt;&lt;li&gt;Increase link popularity by &lt;span style="font-weight:bold;"&gt;submitting the RSS feed&lt;/span&gt;: this is the list of &lt;a href="http://www.rss-specifications.com/rss-submission.htm"&gt;RSS Feeds&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Republish your feed &lt;span style="font-weight:bold;"&gt;anywhere&lt;/span&gt;. You can for example include it as HTML or banner in your forum signature&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;Show off your feed circulation using counter&lt;/span&gt;. You can show your RSS subscription counter and/or your page access counter&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Offer &lt;span style="font-weight:bold;"&gt;feed updates via email&lt;/span&gt; to your biggest fans&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Include an &lt;span style="font-weight:bold;"&gt;auto-discovery tag&lt;/span&gt; in the HTML header of each web page&lt;/li&gt;&lt;br /&gt;&lt;li&gt;If you want to promote your blog using an article you have tu use &lt;span style="font-weight:bold;"&gt;permalinks&lt;/span&gt;. You can use it also to cross-reference your posts&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Use &lt;span style="font-weight:bold;"&gt;linkbacks&lt;/span&gt; to notify when somebody links to one of your documents. You can use &lt;span style="font-weight:bold;"&gt;pingbacks, refbacks and trackbacks&lt;/span&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Build a &lt;span style="font-weight:bold;"&gt;good Buzz Marketing&lt;/span&gt;: you can register to directory services or build your social network. You can read &lt;a href="http://www.buzzmarketingwithblogs.com/"&gt;Buzz Marketing with Blogs by Susannah Gardner &lt;/a&gt; or &lt;a href="http://www.businessweek.com/technology/content/apr2006/tc20060417_582619.htm?chan=smallbiz_smallbiz+index+page_getting+started"&gt;this Business Week article&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Use &lt;span style="font-weight:bold;"&gt;RSS announcers&lt;/span&gt; like &lt;a href="http://rss-announcer.info/"&gt;RSS Announcer&lt;/a&gt; or free services like &lt;a href="http://www.feedburner.com/fb/a/publishers/pingshot;jsessionid=69C267E841F92C6709B87D17227C2DF6.fb1"&gt;FeedBurner PingShot&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;The last advice:&lt;blockquote&gt;subscribe to a &lt;span style="font-weight:bold;"&gt;web analytics service&lt;/span&gt; like &lt;a href="http://www.shinystat.com"&gt;ShinyStat&amp;#8482&lt;/a&gt;. It's free and you can check what sites link you and what search terms are successful in search engines.&lt;br /&gt;&lt;/blockquote&gt;Most of these advices can realized using &lt;a href="http://www.feedburner.com"&gt;FeedBurner&lt;/a&gt;'s services.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8939389355392390587-629653026580872104?l=programminghacks.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/629653026580872104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8939389355392390587&amp;postID=629653026580872104' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/629653026580872104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/629653026580872104'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/2006/11/how-to-optimize-and-promote-your-rss.html' title='How to Optimize your RSS Feed and Promote your Blog or Site'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_jbPuzDFEguM/RXXzjabdUNI/AAAAAAAAAAM/vWuqroeM_wM/s72-c/feed-icon-256x256.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8939389355392390587.post-7112332973921073371</id><published>2006-11-25T18:23:00.000+01:00</published><updated>2008-12-09T02:07:19.617+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='latex'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='pdf'/><category scheme='http://www.blogger.com/atom/ns#' term='unix'/><title type='text'>Single-page PDF Linux Commands</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_jbPuzDFEguM/RXX3oabdUOI/AAAAAAAAAAY/DzGOOWlfga4/s1600-h/linux-penguin.jpg"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_jbPuzDFEguM/RXX3oabdUOI/AAAAAAAAAAY/DzGOOWlfga4/s400/linux-penguin.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5005178834553491682" /&gt;&lt;/a&gt;&lt;br /&gt;I've created a single page PDF containing a Linux Command-Line List.&lt;br /&gt;I think it's very useful to have all commands in a single printable page.&lt;br /&gt;&lt;br /&gt;I hope you'll like it. This is the PDF:&lt;br /&gt;&lt;a href="http://riccardopietrucci.googlepages.com/LinuxCommandsList.pdf"&gt;LinuxCommandsList.pdf&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you want to modify it, you can edit &lt;a href="http://riccardopietrucci.googlepages.com/LinuxCommandsList.tex"&gt;the source file&lt;/a&gt; written in &lt;a href="http://www.latex-project.org/"&gt;LaTeX&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;To build the PDF you have to execute:&lt;br /&gt;&lt;pre&gt;latex LinuxCommandsList.tex&lt;br /&gt;dvips -t landscape LinuxCommandsList.dvi&lt;br /&gt;ps2pdf -sPAPERSIZE=a4 LinuxCommandsList.ps&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8939389355392390587-7112332973921073371?l=programminghacks.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/7112332973921073371/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8939389355392390587&amp;postID=7112332973921073371' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/7112332973921073371'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/7112332973921073371'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/2006/11/linux-command-line-pdf-overview.html' title='Single-page PDF Linux Commands'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_jbPuzDFEguM/RXX3oabdUOI/AAAAAAAAAAY/DzGOOWlfga4/s72-c/linux-penguin.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8939389355392390587.post-2989449058233924890</id><published>2006-11-20T14:52:00.000+01:00</published><updated>2008-12-09T02:07:19.677+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='verification'/><category scheme='http://www.blogger.com/atom/ns#' term='mock'/><category scheme='http://www.blogger.com/atom/ns#' term='assertions'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='unit test'/><title type='text'>Error testing in your software</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_jbPuzDFEguM/RXX4aKbdUPI/AAAAAAAAAAk/jA-EYzwRxoQ/s1600-h/exception.png"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_jbPuzDFEguM/RXX4aKbdUPI/AAAAAAAAAAk/jA-EYzwRxoQ/s400/exception.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5005179689251983602" /&gt;&lt;/a&gt;&lt;br /&gt;When you write methods, you should:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;throw an &lt;span style="font-weight:bold;"&gt;exception &lt;/span&gt;with &lt;span style="font-style:italic;"&gt;public method&lt;/span&gt; when preconditions or postconditions of the &lt;span style="font-style:italic;"&gt;exposed contract&lt;/span&gt; fail&lt;/li&gt;&lt;br /&gt;&lt;li&gt;use &lt;b&gt;assertions&lt;/b&gt; to verify &lt;span style="font-style:italic;"&gt;nonpublic method&lt;/span&gt;'s preconditions and postconditions. For example:&lt;br /&gt;&lt;pre&gt;/* Set coordinates of image i in layout */&lt;br /&gt;void setImageLayout(const Image&amp; i, const int point[]) {&lt;br /&gt;    /* Preconditions: image already loaded and displayed &lt;br /&gt;     * and coordinates are within bounds */&lt;br /&gt;    assert(i.isLoaded() &amp;&amp; i.inLayout());&lt;br /&gt;    assert(point[0] &gt;= 0 &amp;&amp; point[0] &lt;= Window::WindowLimit);&lt;br /&gt;    assert(point[1] &gt;= 0 &amp;&amp; point[1] &lt;= Window::WindowLimit);&lt;br /&gt;&lt;br /&gt;    /* Set layout */&lt;br /&gt;    layout_-&gt;setImage(i,point);&lt;br /&gt;&lt;br /&gt;    /* Postconditions: image layout updated */&lt;br /&gt;    assert(i.isUpdated());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Assertions are usually implemented so that &lt;span style="font-style:italic;"&gt;they can be enabled or disable&lt;/span&gt;d. If assertions are disabled, assertion failures are &lt;span style="font-style:italic;"&gt;ignored&lt;/span&gt;. When the program is released, they are often disabled (while exceptions of public methods continue their checking work...)&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;use &lt;span style="font-weight:bold;"&gt;unit test&lt;/span&gt; to validate that a &lt;span style="font-style:italic;"&gt;particular module of source code&lt;/span&gt; is working properly from each modification to the next. &lt;br /&gt;&lt;a href="http://www.extremeprogramming.org/"&gt;Extreme Programming&lt;/a&gt; states "&lt;a href="http://www.extremeprogramming.org/rules/testfirst.html"&gt;Code the Unit Test First&lt;/a&gt;": in development cycle you'll write tests, code, tests, code, tests, code, ... &lt;br /&gt;You can use many tools like &lt;a href="http://www.junit.org/index.htm"&gt;JUnit&lt;/a&gt;, &lt;a href="http://testng.org/"&gt;TestNG&lt;/a&gt;, &lt;a href="http://cunit.sourceforge.net/"&gt;CUnit &lt;/a&gt;or &lt;a href="http://cppunit.sourceforge.net/cppunit-wiki"&gt;CPPUnit&lt;/a&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Remember this is important rule: &lt;span style="font-weight:bold;"&gt;A test is &lt;span style="font-style:italic;"&gt;not&lt;/span&gt; a unit test if&lt;/span&gt;:&lt;br /&gt;&lt;ul&gt;&lt;li&gt; It talks to the database (try a look for &lt;a href="http://www.dbunit.org/"&gt;DbUnit&lt;/a&gt;)&lt;br /&gt;&lt;li&gt; It communicates across the network&lt;br /&gt;&lt;li&gt; It touches the file system&lt;br /&gt;&lt;li&gt; It can't run at the same time as any of your other&lt;br /&gt;unit tests&lt;br /&gt;&lt;li&gt; You have to do special things to your environment&lt;br /&gt;(such as editing config files) to run it&lt;br /&gt;&lt;li&gt; You are not testing a class in isolation from other&lt;br /&gt;concrete classes: constructs such as &lt;span style="font-weight:bold;"&gt;mock objects&lt;/span&gt; can assist in separating unit tests (with tools like &lt;a href="http://www.easymock.org/"&gt;EasyMock&lt;/a&gt; and &lt;a href="http://mockpp.sourceforge.net/"&gt;mockpp&lt;/a&gt;)&lt;br /&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8939389355392390587-2989449058233924890?l=programminghacks.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/2989449058233924890/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8939389355392390587&amp;postID=2989449058233924890' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/2989449058233924890'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/2989449058233924890'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/2006/11/how-to-verify-your-software.html' title='Error testing in your software'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_jbPuzDFEguM/RXX4aKbdUPI/AAAAAAAAAAk/jA-EYzwRxoQ/s72-c/exception.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8939389355392390587.post-5251108159351149473</id><published>2006-11-14T09:24:00.000+01:00</published><updated>2006-12-06T00:00:18.764+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='memory leak'/><title type='text'>Memory Leaks in C++: important rules</title><content type='html'>This topic &lt;span style="font-weight: bold;"&gt;seems obvious, but it isn't&lt;/span&gt;!! Many memory leaks errors derive from these errors!!&lt;br /&gt;&lt;br /&gt;So, there are some rules you must consider:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt; In polymorphic base class you must declare destructors virtual&lt;/span&gt;. If you don't, the results are undefined. Typically, the derived part of the object is &lt;span style="font-style: italic;"&gt;never destroyed (memory leak)&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;The same analysis applies to any class lacking a virtual destructor&lt;/span&gt;, including &lt;span style="font-family:courier new;"&gt;std::string&lt;/span&gt; and all the STL container types. You &lt;span style="font-style: italic; font-weight: bold;"&gt;do never inherit from a standard container of from any other class with a non-virtual destructor&lt;/span&gt;!!&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Destructors should never emit exception&lt;/span&gt;. This situation can arise the problem of &lt;span style="font-style: italic;"&gt;simultaneously active exceptions&lt;/span&gt;: the result is undefined. There are three primary ways to avoid the trouble:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;Terminate the program&lt;/span&gt; calling abort&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;Swallow the exception&lt;/span&gt; (but it suppresses that something failed)&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;Provide a regular function that &lt;span style="font-weight: bold;"&gt;gives the opportunity to react&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;to problems that may arise (i.e., storing the result of destructor in a boolean variable)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li style="font-weight: bold;"&gt;Have assignment operators return a reference to *this&lt;/li&gt;&lt;li&gt;To make sure that a resource is always released, we need to &lt;span style="font-weight: bold;"&gt;put that resource inside an object&lt;/span&gt; (&lt;span style="font-style: italic;"&gt;a resource manager&lt;/span&gt;) whose destructor will automatically release the resource. This idea is often called &lt;span style="font-style: italic;"&gt;Resource Acquisition Is Initialization &lt;/span&gt;(&lt;span style="font-weight: bold;"&gt;RAII&lt;/span&gt;).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;A &lt;span style="font-weight: bold;"&gt;smart pointer &lt;/span&gt;SP is a wrapper for a resource (i.e., &lt;span style="font-weight: bold;font-family:courier new;" &gt;std::auto_ptr&lt;/span&gt;). It's destructor automatically calls &lt;span style="font-family:courier new;"&gt;delete&lt;/span&gt; on what it points to. Attention: &lt;span style="font-weight: bold;"&gt;copying &lt;/span&gt;&lt;span style="font-weight: bold;font-family:courier new;" &gt;auto_ptr &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;set it to null&lt;/span&gt; and the copying pointer assumes &lt;span style="font-weight: bold;"&gt;sole ownership&lt;/span&gt; of the resource!&lt;br /&gt;&lt;/li&gt;&lt;li&gt;A&lt;span style="font-weight: bold;"&gt; reference-counting smart pointer&lt;/span&gt; RCSP (i.e., &lt;span style="font-weight: bold;font-family:courier new;" &gt;std::tr1::shared_ptr&lt;/span&gt;) is a smart pointer that keeps track of how many objects point to a particular resource and &lt;span style="font-style: italic;"&gt;automatically deletes the resource when nobody is pointing to it any longer&lt;/span&gt;. It also &lt;span style="font-style: italic;"&gt;supports &lt;span style="font-weight: bold;"&gt;custom deleters&lt;/span&gt;&lt;/span&gt;: this prevents the cross-DLL problem and can be used to automatically unlock mutexes.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;SP and RCSP &lt;span style="font-weight: bold;"&gt;use delete, not delete[]&lt;/span&gt;. For arrays, look to &lt;a href="http://www.boost.org/"&gt;&lt;span style="font-weight: bold;"&gt;Boost&lt;/span&gt; &lt;/a&gt;classes: &lt;a href="http://www.boost.org/libs/smart_ptr/scoped_array.htm"&gt;boost::scoped_array&lt;/a&gt; and &lt;a href="http://www.boost.org/libs/smart_ptr/shared_array.htm"&gt;boost::shared_array&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Initialize SPs in standalone statements&lt;/span&gt; (not in complex operations). Failure to do this can lead to subtle resource leaks when eceptions are thrown&lt;/li&gt;&lt;li&gt;By default, C++ passes object by value:&lt;br /&gt;&lt;pre&gt;void viewImage(Image i) {&lt;br /&gt; i.view();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This is an expensive operation: function parameters are copies of the actual arguments! &lt;span style="font-style: italic;"&gt;To bypass all related contructions and destructions, you have to &lt;span style="font-weight: bold;"&gt;pass by reference-to-const&lt;/span&gt;&lt;/span&gt;:&lt;br /&gt;&lt;pre&gt;void viewImage(const Image&amp; i) {&lt;br /&gt; i.view();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Passing parameters by reference &lt;span style="font-style: italic;"&gt;also avoids the &lt;span style="font-weight: bold;"&gt;slicing problem&lt;/span&gt;&lt;/span&gt; (when you don't use virtual function in polymorphic classes).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;It's &lt;span style="font-weight: bold;"&gt;instead &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;more efficient to pass by value&lt;/span&gt; than by reference the following items:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;built-in types (e.g. an int)&lt;/li&gt;&lt;li&gt;iterators in STL&lt;/li&gt;&lt;li&gt;function objects in STL&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Prefer &lt;span style="font-weight: bold;"&gt;this way to write functions that return a new object&lt;/span&gt;:&lt;br /&gt;&lt;pre&gt;inline const Image createImage(const Bitmap&amp; bmp) {&lt;br /&gt; return Image(bmp.getName(), bmp.getData());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;It's faster than you expected and prevents many errors.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Remember that you incur:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;the &lt;span style="font-weight: bold;"&gt;cost of construction&lt;/span&gt; when control reaches the &lt;span style="font-weight: bold;"&gt;variable's definition&lt;/span&gt;&lt;/li&gt;&lt;li&gt;the &lt;span style="font-weight: bold;"&gt;cost of destruction&lt;/span&gt; when variable goes &lt;span style="font-weight: bold;"&gt;out of scope&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;You should &lt;span style="font-weight: bold;"&gt;postpone the definition until you have initialization arguments for it&lt;/span&gt;.&lt;br /&gt;&lt;li&gt;In loops:&lt;br /&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;it's &lt;span style="font-weight: bold;"&gt;more efficient&lt;/span&gt; define variables outside the loop and make an assignment to it on each loop iteration&lt;/li&gt;&lt;li&gt;it's &lt;span style="font-weight: bold;"&gt;more readable&lt;/span&gt; define variables inside the loop. Unless you're dealing with a performance-sensitive part of your code, you should using this approach.&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Don't inline everything!&lt;/span&gt; Consider the &lt;span style="font-style: italic; font-weight: bold;"&gt;rule of 80-20&lt;/span&gt;: a typical program spends 80% of its time executing only 20% of its code. You goal as a software developer is to &lt;span style="font-style: italic;"&gt;identify the 20% of your code&lt;/span&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;This work comes from the &lt;span style="font-weight: bold;"&gt;Scott Meyers's book "Effective C++ - Third Edition"&lt;/span&gt;&lt;br /&gt;Very interesting the &lt;a href="http://www.linuxdevcenter.com/pub/a/linux/2003/05/08/cpp_mm-1.html?page=1"&gt;George Belotsky's article&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8939389355392390587-5251108159351149473?l=programminghacks.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminghacks.blogspot.com/feeds/5251108159351149473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8939389355392390587&amp;postID=5251108159351149473' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/5251108159351149473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8939389355392390587/posts/default/5251108159351149473'/><link rel='alternate' type='text/html' href='http://programminghacks.blogspot.com/2006/11/memory-leaks-in-c-important-rules.html' title='Memory Leaks in C++: important rules'/><author><name>Ricibald</name><uri>http://www.blogger.com/profile/08015630194454598100</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://riccardopietrucci.googlepages.com/RiccardoPietrucciFoto.JPG'/></author><thr:total>7</thr:total></entry></feed>
