<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My Ruminations on Software</title>
	<atom:link href="http://lispmachine.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lispmachine.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sat, 21 Jan 2012 06:08:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='lispmachine.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/18571c40883a523173a8d10ae8708cb0?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>My Ruminations on Software</title>
		<link>http://lispmachine.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://lispmachine.wordpress.com/osd.xml" title="My Ruminations on Software" />
	<atom:link rel='hub' href='http://lispmachine.wordpress.com/?pushpress=hub'/>
		<item>
		<title>2011 in review</title>
		<link>http://lispmachine.wordpress.com/2012/01/07/2011-in-review/</link>
		<comments>http://lispmachine.wordpress.com/2012/01/07/2011-in-review/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 06:25:22 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
		
		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=573</guid>
		<description><![CDATA[The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog. Here&#8217;s an excerpt: The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 14,000 times in 2011. If it were a concert at Sydney Opera House, it would take about 5 sold-out performances for that many [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=573&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.</p>
<p>	<a href="/2011/annual-report/"><img src="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/emailteaser.jpg" width="100%" alt="" /></a></p>
<p>Here&#8217;s an excerpt:</p>
</p>
<blockquote><p>The concert hall at the Sydney Opera House holds 2,700 people.  This blog was viewed about <strong>14,000</strong> times in 2011.  If it were a concert at Sydney Opera House, it would take about 5 sold-out performances for that many people to see it.</p></blockquote>
<p><a href="/2011/annual-report/">Click here to see the complete report.</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/573/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/573/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/573/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=573&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2012/01/07/2011-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>

		<media:content url="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/emailteaser.jpg" medium="image" />
	</item>
		<item>
		<title>Bitwise-Shifting in C</title>
		<link>http://lispmachine.wordpress.com/2011/12/01/bitwise-shifting-in-c/</link>
		<comments>http://lispmachine.wordpress.com/2011/12/01/bitwise-shifting-in-c/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 06:41:29 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[bit operators in C]]></category>
		<category><![CDATA[bit shifting]]></category>
		<category><![CDATA[bitwise operators]]></category>
		<category><![CDATA[C language]]></category>
		<category><![CDATA[C programming]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=555</guid>
		<description><![CDATA[In my two and a half years stint with C, I was never able to comprehend what bitwise operator do. A few days back I was searching for top C interview questions and I came across this piece of code: =============== OUTPUT ==================== /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra interview-2.c /home/arnuld/programs/C $ ./a.out fffffff0 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=555&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
In my two and a half years stint with C, I was never able to comprehend what bitwise operator do. A few days back I was searching for top C interview questions and I came across this piece of code:<br />
<br />
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;

int main(void)
{
  printf(&quot;%x\n&quot;, -1&lt;&lt;4);

  return 0;

}
</pre></p>
<blockquote>
<p>=============== OUTPUT ====================<br />
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra interview-2.c<br />
/home/arnuld/programs/C $ ./a.out<br />
fffffff0<br />
/home/arnuld/programs/C $
</p></blockquote>
<p align="justify">
If you change <em>%x</em> to <em>%d</em>, you will get <em>-16</em> as result, <em>fffffff0</em> is just hexadecimal representation of it. You see that I am using strict ANSI mode and there are no warnings, program compiles fine and gives output as fine as input. Did you notice something wrong ?
</p>
<p align="justify">
Google for n1256.pdf, draft of the ANSI standard and it will be sufficient for your needs if your need is to become a C programmer following ANSI standard and this is what I do. n1256.pdf in Section 6.5.7 (4) states:</p>
<blockquote><p>
  The result of E1 &lt;&lt; E2 is E1 left shifted E2 bit positions; vacated bits are filled with zeroes. If E1 has unsigned type, the result is E1x2^E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1x2^E2 is representable in the result type, then that is resulting value; otherwise , the behavior is undefined.</p></blockquote>
<p>If you pick up a copy of <em>C &#8211; A Reference manual by Harbison and Steele 5th editon</em>, lovingly known as H&amp;S5, you will see section 7.6.3 states: </p>
<ol>
<li>
Applying the shift operator &gt;&gt; is not portable when the left operand is negative, signed value and the right operand is nonzero. </li>
<li>The result value of shift operator is undefined if the value of right operand is negative. The result value is also undefined if the valu eof right operand is greater than or equal to the width (in bits) of the value of the converted left operand.</li>
<li>
The right operand may be zero, in which case no shift occurs and result value is identical to the value of the left operand
</li>
<li>
Each operand must be an integral type. The usual unary conversions are performed on each operand and the result is the converted left operand. The result is not an lvalue.
</li>
</ol>
<p align="justify">
Did you notice how many things you learned by oberving simple one line program ? Almost 5 basic distinctions before you start using bit-shifting in production environment. I have seen programmers in India (both CS engineering students and professionals doing C programming for almost a decade) always trying to srtive for writing bigger and complex programs and <strong>almost always</strong> avoiding all these small details of how any one of the C&#8217;s basic data structues or operators work. They almost hate going into basics. At the same time I observed masters and C experts writing small, terse, succinct and simpler programmes. I follow the masters. Who does not want to become a good C programmer ?
</p>
<p>It is proved that this interview code has undefined behavior. (unfortunately it was a MCQ (Multiple Choice Question) and undefined behavior was not one of the options. You can Google for this question and see the answers for yourself ).
</p>
<p align="justify">
The above problem is just half of the story. <strong>Ben Bacarisse</strong> on <em>comp.lang.c</em> told me the other half of the story. The &#8220;%x&#8221; inside printf() expects an unisgned int argument but interview questions is passing it a signed value hence causing Undefined Behavior (UB in short). Hence above interview code is broken 2 times. Replacing that printf with <em>printf(&#8220;%x\n&#8221;, (unsigned)-1&lt;&lt;4);</em> would have fixed both problems. You can see the original discussion at <a href="http://groups.google.com/group/comp.lang.c/browse_thread/thread/79fe0df7e5021c50#">Google groups</a> and <a href="http://www.rhinocerus.net/forum/lang-c/690320-shifting-bits.html">Rhinocerus forums</a> and <a href="http://www.velocityreviews.com/forums/t806391-shifting-bits.html">Velocity Reviews Forums</a> using your web browser. Once again they all represent web-inerface to Usenet newsgroup comp.lang.c. I use PAN for reading comp.lang.c. Google for the list of news readeing softwares.
</p>
<p align="justify">
In fact, this another program is god for understanding how bit shifting works. Again I got it from comp.lang.c discussions:<br />
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;

enum { LIMIT = 16 };

int main(void)
{
  int i;

  for(i = 0; i &lt;= LIMIT; ++i)
    {
      printf(&quot;%d is shifted left by %d bits, place %s = %d\n&quot;, 100, i, i==1?&quot;&quot;:&quot;s&quot;, 100&lt;&lt;i);
    }

  return 0;
}
</pre></p>
<blockquote><p>===================== OUTPUT =====================<br />
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra bitshift.c<br />
[arnuld@dune C]$ ./a.out<br />
100 is shifted left by 0 bits, place s = 100<br />
100 is shifted left by 1 bits, place  = 200<br />
100 is shifted left by 2 bits, place s = 400<br />
100 is shifted left by 3 bits, place s = 800<br />
100 is shifted left by 4 bits, place s = 1600<br />
100 is shifted left by 5 bits, place s = 3200<br />
100 is shifted left by 6 bits, place s = 6400<br />
100 is shifted left by 7 bits, place s = 12800<br />
100 is shifted left by 8 bits, place s = 25600<br />
100 is shifted left by 9 bits, place s = 51200<br />
100 is shifted left by 10 bits, place s = 102400<br />
100 is shifted left by 11 bits, place s = 204800<br />
100 is shifted left by 12 bits, place s = 409600<br />
100 is shifted left by 13 bits, place s = 819200<br />
100 is shifted left by 14 bits, place s = 1638400<br />
100 is shifted left by 15 bits, place s = 3276800<br />
100 is shifted left by 16 bits, place s = 6553600<br />
[arnuld@dune C]$
</p></blockquote>
<p>You will see everytime you shift a bit left, result is as if input (100) multiplied by 2. Hence if you shift 2 bits, the output is 100 * 2^2 = 400 which gives you precise idea of what the output will be without even writing a program <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . Now ask yourself (without modifying the program) what right shifting will result in ?
</p>
<p align="justify">
<em><br />
Copyright © 2011 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/555/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=555&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2011/12/01/bitwise-shifting-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>Insertion Sort</title>
		<link>http://lispmachine.wordpress.com/2011/09/07/insertion-sort/</link>
		<comments>http://lispmachine.wordpress.com/2011/09/07/insertion-sort/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 13:33:27 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[insertion sort]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=540</guid>
		<description><![CDATA[I always had a hard time understanding algorithms, so I tried to understand them and failed to understand them. This cycle repeated for at least 2 years. I always wanted to have solid grounding in algorithms and data structures. After 3 years of working in Phonologies I finally got my head around many programming skills. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=540&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
I always had a hard time understanding algorithms, so I tried to understand them and failed to understand them. This cycle repeated for at least 2 years. I always wanted to have solid grounding in algorithms and data structures. After 3 years of working in <a href="http://www.phonologies.com/">Phonologies</a> I finally got my head around many programming skills. With that experience in my bag and my frustration of failing everytime from last 2 years, I decided to give algorithms a super-hard try again. I decided I will go hard-headed into algorithms like a man who will eat, sleep, drink and cry algorithms and nothing else. Just dig seep and get my whole body dirty in the process of digging like a labour man. Took me a few months to find out a book that <a href="http://mitpress.mit.edu/algorithms/">fits my mindset</a>. Its written in a very simple and straighforward manner and even after that I felt a lot of difficulty in understanding first few chapters. And finally I am able to get my head around algorithms. Have read first three chapters and insetion sort is given as an example while selection sort is given an exercise which Iw as able to solve. Here is my take on Insertion Sort, on how I understood it:
</p>
<p align="justify">
This is Insertion Sort from <a href="http://en.wikipedia.org/wiki/Insertion_sort">Wikipedia</a> which is similar to Cormen&#8217;s book:<br />
<pre class="brush: plain;">
for j = 1 to length(A)-1
     key = A[ j ]
      i = j - 1
     while i &gt;= 0 and A [ i ] &gt; key
         A[ i +1 ] = A[ i ]
         i = i -1
     A [i +1] = key
</pre>
</p>
<p align="justify">
Felt too much confused with this, I tried to understand many times but never did. Did not find anyone explaining in the way I understand, hence I tried to use a C array of integers to understand and came up with this:</p>
<ol>
<li>Take <strong>A = {3,5,2}</strong> as array to apply Insertion Sort pseudo code</li>
<li><strong>first 3 lines:</strong> j = 1, key = 5, i = 0 </li>
<li><strong>while loop will not execute as A[0] = 3 and 3 &gt; 5 is not true </strong> A = {3,5,2}  </li>
<li><strong>Last line:</strong> A = {3,5,2} </li>
<li><strong>Outer loop 2nd turn, first three lines:</strong> j = 2, key = 2 , i = 1</li>
<li><strong>After inner while loop, first round </strong> A = {3,2,5}, i = 0 </li>
<li><strong>After inner while loop, second round </strong> A = {3,3,5}, i = -1 </li>
<li><strong>Last line:</strong> {2,3,5} </li>
<li><strong>Outer Loop finishes </strong> array is sorted</li>
</ol>
<p align="justify">
I am not a child prodigy hence it took me 3 hours to come up with an array with right elements in place. I did 3 pages of paper work with wrong values and wrong indices before I got them right. (please add 5 more days where I tried really haaaaaaaard to understand from the book). Hard part was not understanding the example of playing cards. Cormen&#8217;s book uses playing cards example to show insertion sort (special thanks to the authors as that picture of a hand sorting cards really helped). Real life sorting of playing card is very easy, I have done it a thousand times without thinking. Translating that practical approach into pseudo code was the hard part.
</p>
<p align="justify">
<em><br />
Copyright © 2011 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/540/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=540&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2011/09/07/insertion-sort/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>The Way of C++ Problem Solving</title>
		<link>http://lispmachine.wordpress.com/2011/08/09/way-of-cpp/</link>
		<comments>http://lispmachine.wordpress.com/2011/08/09/way-of-cpp/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 06:06:50 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=525</guid>
		<description><![CDATA[Many programmers write their programs in C and then save the program with extension .cpp and call it C++ program. I am one of them, except that I don&#8217;t call those programs C++ programs. I don&#8217;t know have much knowledge of C++, mostly work in C. Over time I have read so many articles and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=525&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
Many programmers write their programs in C and then save the program with extension <em>.cpp</em> and call it C++ program. I am one of them, except that I don&#8217;t call those programs C++ programs. I don&#8217;t know have much knowledge of C++, mostly work in C. Over time I have read so many articles and blogposts that I am fully convinced beyond any doubt that C++ and C are two similar looking and very different languages. They were designed with different aims, they have entirely different philosophies about designing a program. Unlike what most programmers say that C anc C++ are brothers, I will say they are not even far distant cousins. C and C++ are 2 persons who belong to different nationality and have different skin color and they speak different languages. To know the true way of C++ I thought of writing a C++ program which reads a file and saves that into a <em>std::string</em> (This is first lesson, use std:: string instead of <em>using namespace std</em> which defeats the aim of namespaces. I posted the discussion my probblem on comp.lang.c++ which you can read on archives like rhinocerus and velocityreviews: </p>
<p><a href="http://www.velocityreviews.com/forums/t752581-reading-a-file-into-std-string.html" title="http://www.velocityreviews.com/forums/t752581-reading-a-file-into-std-string.html">http://www.velocityreviews.com/forums/t752581-reading-a-file-into-std-string.html</a></p>
<p><a href="http://www.rhinocerus.net/forum/language-c/682219-reading-file-into-std-string.html" title="http://www.rhinocerus.net/forum/language-c/682219-reading-file-into-std-string.html">http://www.rhinocerus.net/forum/language-c/682219-reading-file-into-std-string.html</a></p>
<p>I wrote the code as a C Programmer who did some google search and read a few pages of Stroustrup.  Red Floyd posted a true C++ version.  You see how much the difference in thinking, Red Floyd&#8217;s code is much superior than mine. You can easily conclude that C++ what you know or what your colleagues know is not really true C++. This program made me think harder about my learning and experience as a computer programmer. Solving problems in C++ is really vastly different from the way we do it in C. Don&#8217;t write C code and save the file with extension <em>.cpp</em>. If you want to write C code then please save the file as <em>.c</em> Lessons learned
</p>
<p><pre class="brush: cpp;">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;


int main()
{
  std::string my_contents, tmp_contents;
  std::ifstream my_file(&quot;reference.cpp&quot;);
  if(!my_file)
    {
      std::cerr &lt;&lt; &quot;Error Opening file&quot; &lt;&lt; std::endl;
      exit(EXIT_FAILURE);
    }

   while(my_file)
    {
     std::getline(my_file, tmp_contents);
     my_contents += tmp_contents;
     my_contents += &quot;\n&quot;;
    }

  std::cout &lt;&lt; &quot;String contents are: &quot;&lt;&lt; &quot;\n&quot;
	    &lt;&lt; my_contents &lt;&lt; std::endl;

  my_file.close();


  return 0;
}
</pre></p>
<p align="justify"> &nbsp; </p>
<p><pre class="brush: cpp;">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;


int main()
{
  std::ifstream my_file(&quot;reference.cpp&quot;);
  if(!my_file)
    {
      std::cerr &lt;&lt; &quot;Error Opening file&quot; &lt;&lt; std::endl;
      exit(EXIT_FAILURE);
    }

  /* This was posted by &quot;redfloyd&quot; on comp.lang.c++ */
  std::string my_contents(std::istreambuf_iterator&lt;char&gt;(my_file.rdbuf()), (std::istreambuf_iterator&lt;char&gt;()));

  std::cout &lt;&lt; &quot;String contents are: &quot;&lt;&lt; &quot;\n&quot;
	    &lt;&lt; my_contents &lt;&lt; std::endl;
  return 0;
}
</pre></p>
<p align="justify"> &nbsp; </p>
<p align="justify">
<em><br />
Copyright © 2011 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/525/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=525&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2011/08/09/way-of-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>Queue using doubly linked-list</title>
		<link>http://lispmachine.wordpress.com/2011/06/13/queue-using-doubly-linked-list/</link>
		<comments>http://lispmachine.wordpress.com/2011/06/13/queue-using-doubly-linked-list/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 14:01:55 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=510</guid>
		<description><![CDATA[Here is what I implemented as Queue using doubly linked-list. Short name will be FIFO (First-In-First-Out). remove_element() was corrected by Ike Naar on comp.lang.c and hence I included his version (better than mine) as remove_element_2() . Learning algorithms and data structures is always a tough thing to do but thats the only way to become [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=510&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
Here is what I implemented as Queue using doubly linked-list. Short name will be FIFO (First-In-First-Out). <em>remove_element()</em> was  corrected by Ike Naar on comp.lang.c and hence I included <a href="http://groups.google.com/group/comp.lang.c/msg/04c33699c02d9e85?dmode=source">his version</a> (better than mine) as <em>remove_element_2()</em> . Learning algorithms and data structures is always a tough thing to do but thats the only way to become a good programmer <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
<p align="justify">
<strong>NOTE:</strong> A Queue is used for adding elemnts at tail and removing them from head. Its not a good idea to remove elements from anywhere except head. Do not use use <em>dequeu_anywhere()</em> in production code as it is perfectly capable of causing efficiency issues in your software. Richard Heathfield once said, they are not for practical use but good for learning, as a mental exercise.
</p>
<p><pre class="brush: cpp;">
/* A Queue (FIFO)  implementation using doubly-linked list. All operations are based on page 70, section 2.4 &quot;Data Structures and 
 *   Algorithms by Aho, Hopcraft and Ullman&quot;.
 *
 * VERSION 0.0
 *
 */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

enum { VAL_SUCC = 0, VAL_ERR = 1};

struct dlqueue
{
  int num;
  struct dlqueue* next;
  struct dlqueue* prev;
};


struct dlqlist
{
  struct dlqueue* head;
  struct dlqueue* tail;
};



int enqueue(struct dlqlist*, int);
int dequeue(struct dlqlist*);
struct dlqueue* front(struct dlqlist*);
void makenull(struct dlqlist*);
int empty(struct dlqlist*);
void print_queue(struct dlqlist* );


int dequeue_anywhere(struct dlqlist*s , int numd);
void remove_element(struct dlqlist* s, struct dlqueue* d);


int main(void)
{
  struct dlqlist* s = malloc(1 * sizeof *s);
  if(NULL == s)
    {
      fprintf(stderr,&quot;IN: %s @%d: Out of Memory\n&quot;, __FILE__, __LINE__);
      return EXIT_FAILURE;
    }
  else
    {
      s-&gt;head = s-&gt;tail = NULL;
    }

  enqueue(s, 10);
  enqueue(s, 11);
  enqueue(s, 12);
  enqueue(s, 13);
  print_queue(s);

  dequeue_anywhere(s,12);
  printf(&quot;\n\n----------------------------\n&quot;);
  print_queue(s);

  /*
  dequeue(s);
  printf(&quot;\n\n----------------------------\n&quot;);
  print_queue(s);

  dequeue(s);
  printf(&quot;\n\n----------------------------\n&quot;);
  print_queue(s);


  dequeue(s);
  printf(&quot;\n\n----------------------------\n&quot;);
  print_queue(s);
  */
  return EXIT_SUCCESS;
}



/* Adds an element to tail of Queue */
int enqueue(struct dlqlist* s, int i)
{
  int ret;
  if(NULL == s)
    {
      fprintf(stderr, &quot;IN: %s @ %d: Invalid Args\n&quot;, __FILE__, __LINE__);
      ret = VAL_ERR;
    }
  else if(NULL == s-&gt;head &amp;&amp; NULL == s-&gt;tail)
    {
      struct dlqueue* p = malloc(1 * sizeof *p);
      if(NULL == p)
	{
	  fprintf(stderr,&quot;IN: %s @%d: Out of Memory\n&quot;, __FILE__, __LINE__);
	  ret = VAL_ERR;
	}
      else
	{
	  p-&gt;num = i;
	  p-&gt;prev = p-&gt;next = NULL;

	  s-&gt;head = s-&gt;tail = p;
	  ret = VAL_SUCC;
	}
    }
  else if(NULL == s-&gt;head || NULL == s-&gt;tail)
    {
      fprintf(stderr, &quot;IN: %s @%d: Serious error.&quot;, __FILE__, __LINE__);
      fprintf(stderr,&quot;List one of the list's head/tail is null while other is not\n&quot;);
      ret = VAL_ERR;
    }
  else
    {
      struct dlqueue* p = malloc(1 * sizeof *p);
      if(NULL == p)
	{
	  fprintf(stderr,&quot;IN: %s @%d: Out of Memory\n&quot;, __FILE__, __LINE__);
	  ret = VAL_ERR;
	}
      else
	{
	  p-&gt;num = i;
	  p-&gt;prev = p-&gt;next = NULL;

	  s-&gt;tail-&gt;next = p;
	  p-&gt;prev = s-&gt;tail;
	  s-&gt;tail = p;
	  ret = VAL_SUCC;
	}
    }

  return ret;
}



int dequeue(struct dlqlist* s)
{
  int ret;
  if(NULL == s)
    {
      fprintf(stderr, &quot;IN: %s @ %d: Invalid Args\n&quot;, __FILE__, __LINE__);
      ret = VAL_ERR;
    }
  else if(NULL == s-&gt;head &amp;&amp; NULL == s-&gt;tail)
    {
      printf(&quot;Nothing to Dequeue()\n&quot;);
      ret = VAL_SUCC;
    }
  else if(NULL == s-&gt;head || NULL == s-&gt;tail)
    {
      fprintf(stderr, &quot;IN: %s @%d: Serious error.&quot;, __FILE__, __LINE__);
      fprintf(stderr,&quot;List one of the list's head/tail is null while other is not\n&quot;);
      ret = VAL_ERR;
    }
  else
    {
      struct dlqueue* p = s-&gt;head;
      if(NULL == s-&gt;head-&gt;next &amp;&amp; NULL == s-&gt;tail-&gt;next) /* if last element */
	{
	  s-&gt;head = s-&gt;tail = NULL;
	}
      else
	{
	  s-&gt;head = s-&gt;head-&gt;next;
	}

      free(p);
      ret = VAL_SUCC;
    }

  return ret;
}


int dequeue_anywhere(struct dlqlist*s , int numd)
{
  int ret;
  if(NULL == s)
    {
      fprintf(stderr, &quot;IN: %s @ %d: Invalid Args\n&quot;, __FILE__, __LINE__);
      ret = VAL_ERR;
    }
  else if(NULL == s-&gt;head &amp;&amp; NULL == s-&gt;tail)
    {
      printf(&quot;Nothing to Dequeue()\n&quot;);
      ret = VAL_SUCC;
    }
  else if(NULL == s-&gt;head || NULL == s-&gt;tail)
    {
      fprintf(stderr, &quot;IN: %s @%d: Serious error.&quot;, __FILE__, __LINE__);
      fprintf(stderr,&quot;List one of the list's head/tail is null while other is not\n&quot;);
      ret = VAL_ERR;
    }
  else
    {
      struct dlqueue* p = s-&gt;head;
      for(; p; p = p-&gt;next)
	{
	  if(numd == p-&gt;num)
	    {
	      remove_element(s,p);
	    }
	}
      ret = VAL_SUCC;
    }

  return ret;
}


void remove_element(struct dlqlist* s, struct dlqueue* d)
{
  if(NULL == d-&gt;next &amp;&amp; (NULL == s-&gt;head-&gt;next &amp;&amp; NULL == s-&gt;tail-&gt;next)) /* only one element in queue */
    {
      s-&gt;head = s-&gt;tail = NULL;
    }
  else if((NULL == d-&gt;next) &amp;&amp; d-&gt;prev) /* removing tail */
    {
      s-&gt;tail = d-&gt;prev;
      d-&gt;prev-&gt;next = NULL;
    }
  else if(d-&gt;next &amp;&amp; (NULL == d-&gt;prev)) /* removing head */
    {
      s-&gt;head = d-&gt;next;
      s-&gt;head-&gt;prev = NULL;
    }
  else /* removing from center or somewhere */
    {
      d-&gt;prev-&gt;next = d-&gt;next;
      d-&gt;next-&gt;prev = d-&gt;prev;
    }

  free(d);
}

void remove_element_2(struct dlqlist* s, struct dlqueue* d)
{
  if(NULL == d-&gt;next)
    {
      s-&gt;tail = d-&gt;prev;
    }
  else
    {
      d-&gt;next-&gt;prev = d-&gt;prev;
    }

  if(NULL == d-&gt;prev)
    {
      s-&gt;head = d-&gt;next;
    }
  else
    {
      d-&gt;prev-&gt;next = d-&gt;next;
    }

  free(d);


}


void print_queue(struct dlqlist* s)
{
  if(NULL == s)
    {
      fprintf(stderr, &quot;IN: %s @ %d: Invalid Args\n&quot;, __FILE__, __LINE__);
     }
  else if(NULL == s-&gt;head &amp;&amp; NULL == s-&gt;tail)
    {
      printf(&quot;Nothing to print\n&quot;);
    }
  else if(NULL == s-&gt;head || NULL == s-&gt;tail)
    {
      fprintf(stderr, &quot;IN: %s @%d: Serious error.&quot;, __FILE__, __LINE__);
      fprintf(stderr,&quot;List one of the list's head/tail is null while other is not\n&quot;);
    }
  else
    {
      struct dlqueue* p = s-&gt;head;
      while(p)
	{
	  printf(&quot;num = %d\n&quot;, p-&gt;num);
	  p = p-&gt;next;
	}
    }
}

</pre></p>
<p align="justify"> &nbsp; </p>
<p align="justify">
<em><br />
Copyright © 2011 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/510/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/510/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=510&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2011/06/13/queue-using-doubly-linked-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Lessons</title>
		<link>http://lispmachine.wordpress.com/2011/05/12/simple-lessons/</link>
		<comments>http://lispmachine.wordpress.com/2011/05/12/simple-lessons/#comments</comments>
		<pubDate>Thu, 12 May 2011 05:03:54 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structu]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=493</guid>
		<description><![CDATA[I am working in C from last 3 years and these two very simple lessons are based on my experience, these are not taken from any book but by my own understanding of programming. They may look very simple and short and don&#8217;t underestimate them. You may already know them (I did) and you may [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=493&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
I am working in C from last 3 years and these two very simple lessons are based on my experience, these are not taken from any book but by my own understanding of programming. They may look very simple and short and don&#8217;t underestimate them. You may already know them (I did) and you may not be aware of them. Its like everyone knew that apple falls on the ground but only Newton became aware of it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
<p align="justify">
<ul>
<li>
<p align="justify"><strong>Solving Problems:</strong> Programming is all about solving problems. I knew that (and I am sure you knew that too), every programmer know this but how many programmers translate business problems into code?  May be you are working on the problem which is just your interpretation of the problem?  What is the real business problem you are trying to solve on your job as a programmer? You are actually a translator between a business man and computer.  </p>
</li>
<li>
<p align="justify"><strong> Diagrams:</strong> Before you even think about coding you must draw a digram on how you are going to split this business problem into some or lots of small ones and what will be the general idea behind solving this problem. That you can accomplish very effectively by drawing a diagram on paper using a pen (or pencil). Also known as DFDs (<a href="http://en.wikipedia.org/wiki/Data_flow_diagram">Data Flow Diagrams</a>). You will see how much of your life has become easier with DFDs. DFDs follow some rules and you can make your own diagrams and your own rules but make sure you stick to some standard (whether your own or someone else). DFDs are designed to make your life easier, it will make you happy because once you start coding you will see how important they are.  </p>
</li>
<li>
<p align="justify"><strong>Tools: </strong> What tools you are using to translate problems into code ?  Whether you are solving business problems or you do it for joy of solving problems (in the world of Hackers), Algorithms and Data Structures are the first of the tools you need, programming language is 2nd tool. Majority of programmers do not focus on first tool. Why you need qsort() over other sorting techniques. Why AVL tree will be better than Red-Black tree or Binary-Search tree. Why you will use a circular linked list instead of a doubly-linked list implemented as a Stack ?   Why there exist so many different kind of trees and linked lists.   Even I, myself, know only few of them. Its hard work but surely simple and very rewarding.  I think half of the bugs exist in software because programmers lack this tool or the intended use of it</p>
</li>
<li>
<p align="justify"><strong>Tools-2: </strong> Like I said, programming language you are using is 2nd tool. Don&#8217;t go to high level talk here or dig yourself into advanced programming before you understand the basics. I use C language, so before I go into advanced C book, I will make sure I have spent two years  on comprehending <a href="http://c-faq.com/">C FAQs</a> and comp.lang.c<a href="http://groups.google.com/group/comp.lang.c/topics?gvc=2"> archives </a> </p>
</li>
<li>
<p align="justify"><strong>Breaking a big problem into small problems: </strong> I am using C, which is a procedural programming language. Hence I break down a business problem into small procedures (also known as functions). I will make sure that when one procedure calls other  then both of them are doing some specific work and that in a proper and structured way. Writing too many procedures or the ones which do not have a proper siginificance can (and will) cause semantic bugs which in turn cause headaches over time.   </p>
</li>
</ul>
<p align="justify">
<em><br />
Copyright © 2011 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</em></p>
<p><em>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/493/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=493&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2011/05/12/simple-lessons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>Broken Binary Search</title>
		<link>http://lispmachine.wordpress.com/2010/12/28/broken-binary-search/</link>
		<comments>http://lispmachine.wordpress.com/2010/12/28/broken-binary-search/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 11:29:07 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=479</guid>
		<description><![CDATA[While randomly googling for binary search, I came across this article where author (I guess one of the employees of Google) says that nearly all binary searches are mergesorts are broken. I don&#8217;t know much about Java but here is what he is showing on his blog: Author says line 6: int mid =(low + [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=479&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
While randomly googling for binary search, I came across <a href="http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html">this article</a> where author (I guess one of the employees of Google) says that nearly all binary searches are mergesorts are broken.  I don&#8217;t know much about Java but here is what he is showing on his blog:<br />
<pre class="brush: java;">
1:     public static int binarySearch(int[] a, int key) {
2:         int low = 0;
3:         int high = a.length - 1;
4:
5:         while (low &lt;= high) {
6:             int mid = (low + high) / 2;
7:             int midVal = a[mid];
8:
9:             if (midVal &lt; key)
10:                 low = mid + 1
11:             else if (midVal &gt; key)
12:                 high = mid - 1;
13:             else
14:                 return mid; // key found
15:         }
16:         return -(low + 1);  // key not found.
17:     }
</pre>
</p>
<p align="justify">
Author says line 6: <em>int mid =(low + high) / 2;</em> is broken because if sum of low and high is greater than INT_MAX then it will overflow.  From author himself; <em>This bug can manifest itself for arrays whose length (in elements) is 2^30 or greater (roughly a billion elements). This was inconceivable back in the &#8217;80s, when Programming Pearls was written, but it is common these days at Google and other places</em>.  He is right and I am not posting this to say what has already been said. I am interested in line 3: <em>int high = a.length &#8211; 1;</em>
</p>
<p align="justify">
Like I said I don&#8217;t know much about Java and if we write same line of code in C, it will overflow if <em>a.length</em>  is  long, unsigned long or anything larger than  what <em>size_t</em> type can handle. It will give strange results (UB or Undefined Behavior). If having more than <em>size_t</em> elements is common for places like Google then I think line number 3 needs to be the cause of concern too, not just line number 6.  You can know size of array in C with these methods:
</p>
<p align="justify">
 const char* arrc = &#8220;123&#8243;;<br />
 int arri[] = {3,2,1};</p>
<p>first is an array of characters while 2nd is an array of integers. You can use <em>sizeof(arri) / sizeof(arri[0])</em> to know number of elements in <em>arri</em>. To know number of elements in <em>arrc</em> you can use either <em>strlen(arrc)</em> or <em>sizeof(arrc) / sizeof(*arrc) &#8211; 1</em>. Both <em>sizeof</em> and <em>strlen()</em> return <em>size_t</em> type which is unsigned int and hence does not fit in int type.  I have commented this on that blog, lets see what author says.
</p>
<p><em><br />
Copyright © 2010 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</em></p>
<p><em>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.</p>
<p>Copyright does not belong to Java code. Please ask for permission from the author of the following blog before using mentioned Java code: http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/479/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=479&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2010/12/28/broken-binary-search/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>Extract of Programming</title>
		<link>http://lispmachine.wordpress.com/2010/12/08/extract-of-programming/</link>
		<comments>http://lispmachine.wordpress.com/2010/12/08/extract-of-programming/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 06:30:46 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[programmers]]></category>
		<category><![CDATA[proprietary software]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[usenet]]></category>
		<category><![CDATA[programmer]]></category>
		<category><![CDATA[comp.lang.c]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=466</guid>
		<description><![CDATA[A colleague of mine once asked me a question: How to become a better Programmer ?. It was a small but brilliant question. Most programmers do not think of the answer which is correct. A correct answer will also tell you about the extract of programming. I have hanged on comp.lang.c for quite some time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=466&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
A colleague of mine once asked me a question: <em>How to become a better Programmer ?</em>. It was a small but brilliant question. Most programmers do not think of the answer which is correct. A correct answer will also tell you about the extract of programming.   I have hanged on <em>comp.lang.c</em> for quite some time and I have always tried to learn as much as possible from programmers who are more intelligent and better than me. This has led to many changes in my <a href="http://en.wikipedia.org/wiki/Ideology">belief-system</a> on what is right way to program and what is the wrong way. Over time my definition of what are called fundamentals and basics of programming have changed, my practice of programming have taken an almost U-turn from what I used to do. I have become better and the very first sentence of the long answer to the question How to become a better Programmer or What is the essence (extract) of programming, will be <em>Love</em>. yes, good programmers,  brilliant programmers love to program. They have certain kind of <a href="http://www.catb.org/~esr/faqs/hacker-howto.html#attitude">attitude</a> that defines them, that separates them form the rest of <a href="http://c-faq.com/ansi/voidmain.html">void main()s</a>.
</p>
<p align="justify">
I know you will say that everyone just can&#8217;t love programming. Yes, you are right. Trouble is, Indian education system does its best to not to let creative students love computers. The whole academic syllabus will be designed and selection of books will be done in such a way they will make any creative man to hate computers. You have to find your own way, you are on your own, thats bad news. Good news is once you determined to find your way then lot of people will help you, many of them you will never meet in real life. The community of these programmers, hanging out there reading code for hours  can become your favorite pass time.  Real programmers are very helpful friends, instructors and teachers. You will never be spoonfed, you will never be disrespected but only analyzed and encouraged to write good programs, programs that have quality. And one thing I have learned is that qulity comes from writing small programs, not big ones.  I had a misocnception when I was a graduate student (most Indian students do have same) that writing big and complex programs will make you a better programmer, It will not (actually It will but its not true for beginners, you must already be good at basics to start writing big and complex software) Unfortunately most programmers have a weak basic and fundamental grouding in what they do.  Here is the answer:</p>
<ul>
<li> Write short programs. They will make you better.  e.g. think what <em>*p++</em> and <em>*++p</em> will do for a pointer <em>p</em> and what can be the result of <em>*p++ = *++p</em>.</li>
<li>Don&#8217;t just think, write code. Reading books will not take you anywhere. Knowledge alone is essential and it won&#8217;t make you a better programmer. Write code and write more code and write a lot more code.</li>
<li>Write code and read <a href="http://groups.google.com/group/comp.lang.c/topics?gvc=2">archives of comp.lang.c</a> if you get problems. Most of the time solution will already be there. Don&#8217;t just ask for help. Search, read archives and if you can&#8217;t sit for hours reading archives and trying code, find some other profession</li>
<li>Hang onto you favorite language newsgroup. For C we have comp.lang.c, same for others e.g. comp.lang.lisp, comp,land.c++ etc.  you can&#8217;t be a better programmer without hanging out there for a few years.</li>
<li> Be technical rather than personal on usenet. If someone says something that you don&#8217;t like because it goes against what you have learned from your school/college then please get over it. You are not in school anymore, its real world and hence things are done differently here, things are practical here</li>
<li>Rather than learning three or four new languages, give priority to learn basic algorithms and data structures. They are tough, yes, and they are very rewarding when it comes to solving real world problems (which is what you will do in a software job/business).  This approach will save you not only from lot of suffering caused by <a href="http://en.wikipedia.org/wiki/Segmentation_fault">Segmentation Faults</a> but also happiness will come when you see how succinct, meaningful and readable your programs have become. Thats called maturity.
</li>
<li>If you want to learn new language then learn something opposite to what you already know e.g. if you know C or C++ already then rather than learning Java, go learn Common Lisp, If you already know Python, then start learning learn Haskell, if you already know Perl, then learn Ruby to see how it solves the same problems.  If you think you know already know a lot of programming then read <a href="http://mitpress.mit.edu/sicp/full-text/book/book.html">Structure and Interpretation of Computer Programs</a> and tell me how you feel <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>What I have written is  based on my years of experience in programming. I have summarized my experience in a very small and short blog. Don&#8217;t just ignore it. I have done many mistakes as beginner and I don&#8217;t want you to waste your young years in walking a path and later realize that it was wrong. Youth won&#8217;t come again, save it, invest it to proper use. Programming is a kind of love difficult to leave once you got hooked onto living with it. Its a joy but a hard kind of joy <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />
</p>
<p><em><br />
Copyright © 2010 Arnuld Uttre, Village &#8211; Patti, P.O &#8211; Manakpur, Tehsil &#8211; Nangal,  Distt. &#8211; Ropar, Punjab (INDIA)</em></p>
<p><em>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/466/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/466/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=466&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2010/12/08/extract-of-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>The Programmer in Indian Corporate &#8211; 2</title>
		<link>http://lispmachine.wordpress.com/2010/11/18/the-programmer-in-indian-corporate-2/</link>
		<comments>http://lispmachine.wordpress.com/2010/11/18/the-programmer-in-indian-corporate-2/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 08:32:36 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[programmer]]></category>
		<category><![CDATA[programmers]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=461</guid>
		<description><![CDATA[In my earlier rant I talked about how Indian programmers are not interested in learning and sharpening their programming skills. I think did not do a justice by talking only about majority of them. There is a minority who is really interested in learning. What about them ? There are programmers I have met (very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=461&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">
In my <a href="http://lispmachine.wordpress.com/2010/11/15/440/">earlier rant</a> I talked about how Indian programmers are not interested in learning and sharpening their programming skills. I think did not do a justice by talking only about majority of them. There is a minority who is really interested in learning. What about them ?
</p>
<p align="justify">
There are programmers I have met (very few though, you know what minority means) who are interested in sharpening their craft, who are interested in learning programming but the organization does not support such kind of environment. Most MNCs in India do not provide an environment where a programmer can learn basics of programming, they never ever talk to their programmers on why <a href="http://en.wikipedia.org/wiki/Niklaus_Wirth">Niklaus Wirth</a> said <a href="http://www.inf.ethz.ch/personal/wirth/books/AlgorithmE0/">Algorithms + Data Structures = Programs</a>.  All of them are made  busy to meet deadlines, and they have to do it else they will loose their jobs. No one wants to loose his job. There will be hundreds of programmers working on one project and almost 80% of them never know what other one is doing. There will be less than or equal to hundred source code files and those 120 or 100 or 200 programmers will be working on them and on the top of that project manager will not be knowing which data structure (in conjunction with what algorithm) was used for solving the problem and then companies wonder why their proprietary software is called crap quality by programmers who have <a href="http://en.wikipedia.org/wiki/Open_source">Open Source</a> mindset. Programming is about solving problems, solving them in a smart way, solving them with an attitude of diligence and contemplating over small, very small and very tiny parts of code. Big parts, big things do not matter when it comes to problem solving in programming (coding). If you think you should have big programs, big files and need big teams to make a fine piece of software, you are wrong.  Companies already have hired hundreds of programmers, dividing them in teams and then managers are managing them like a herd of cows and they are making software this way since the inception of word software. No wonder they still have problems they were getting 20 years back, testing pahse of the code written by those hundreds is 5 to 10 times more than the coding phase. What a shame. How much it costs ?  The more you do it, the more the price of end product is going to rise.   You have two solutions for this problem</p>
<ul>
<li> <strong>MBA solution:</strong></li>
<p> Filter all the empoyees based on some stupid tests taken from some books that teach wrong ways of programming. Fancy name is cost cutting, real name is jackassing in action.</p>
<li><strong>Technical solution:</strong></li>
<p> Provide an environment for programmers to learn, to sharpen their programming skills. Fire the current manager (especially if he has MBA degree) and hire technical one, he will do the job better. Ask programmers why Niklaus Wirth said <em>Algorithms + Data Structures = Programs</em>. Fire all those programmers who are not willing to learn. If you are a opening a new division of the company, then rather than hiring 100 programmers, add the salaries of all 100 programmers, divide it by 4 and  hire 3 programmers, like <a href="http://www.cpax.org.uk/">Richard Heathfield</a>, <a href="http://www.informatimago.com/">Pascal Bourguignon </a>, <a href="http://groups.google.com/group/comp.lang.c++/browse_thread/thread/9a19e1fa4995d776#">Victor Bazarov</a>, <a href="http://weitz.de/">Edi Weitz</a>, <a href="http://pathfinderpeople.blogs.com/">H. S. Lahman</a> or <a href="http://walfield.org/">Neil Walfield</a>. Pay them that 3 units of &#8216;<em>100 divided by 4&#8242;</em> salary and get a piece of software which your 1000 programmers team can not create in a thousand years and I bet on that. Don&#8217;t forget <a href="http://lispmachine.wordpress.com/about/">Arnuld Uttre</a> for 4th position =:o)
</ul>
</p>
<p align="justify">
Many managers and programmers may (and will) think that I am talking of crap solutions.  Well, You can think anything you want and I speak from experience. I talk real, I write facts. Companies who pay salaries/bonuses on who wrote more lines of code are simply wasting their resources, pay more to them who write less code. Real programs are succinct and short: <strong>for(;;) *p++ = *q++</strong> . In India, there are too many talented programmers who are living their lives on tiny financial amount they get. There are programmers who write crap and get 10 times more salary than the real one. There are very talented programmers who are doing odd jobs because they can&#8217;t get a programming job. Whether you agree or not, a quality piece of software will always be created by a programmer who has a solid grasp of algorithms and data structures and if you don&#8217;t have such kind of programmers then, either you or your management have sheep mentality, just like everyone else.</p>
<p align="justify"> &nbsp; </p>
<p align="justify"> &nbsp; </p>
<p align="justify">
<em><br />
Copyright © 2010 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. &#8211; Ropar, Punjab (INDIA) &#8211; 140126</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/461/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=461&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2010/11/18/the-programmer-in-indian-corporate-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
		<item>
		<title>The Programmer in Indian Corporate</title>
		<link>http://lispmachine.wordpress.com/2010/11/15/440/</link>
		<comments>http://lispmachine.wordpress.com/2010/11/15/440/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 06:00:15 +0000</pubDate>
		<dc:creator>arnuld</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[3rd aprty API]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[libcurl]]></category>

		<guid isPermaLink="false">http://lispmachine.wordpress.com/?p=440</guid>
		<description><![CDATA[Quite a long time I have not posted anything (was unable to hack into programming, and even Linux). Corporate life has taken a toll on precious human life of mine and unfortunately I can&#8217;t do anything about it right now. Was trying to use C API for cURL to get an experience on how to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=440&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify"> Quite a long time I have not posted anything (was unable to hack into programming, and even Linux). Corporate life has taken a toll on precious human life of mine and unfortunately I can&#8217;t do anything about it right now. Was trying to use C API for cURL to get an experience on how to use 3rd party APIs to build something we need. Remember the <a href="http://www.catb.org/~esr/faqs/hacker-howto.html#believe2">Hacker&#8217;s Belief #2</a> . 2nd I am writing this blogpost because I have seen many programmers (developers/software-enginners, or whatever fancy word you will like to use here) just don&#8217;t read the manual properly before using 3rd party API, they just don&#8217;t care, they feel frustration on reading the reference maual or man pages. I never felt so, may be because I started my learning from places like <a href="http://en.wikipedia.org/wiki/Usenet">usenet</a> and was more interested in programming itself rather than what kind of degree I will get from university. I say so because in India, most programmers focus on getting higher marks in computer engineering  than learning basics of programming and later take this attitude to wherver they work. They become so much used to easy and fast way of writing crappy programs that they completely forget the patience in learning programming skills. Its good to impress some employer with 90-95% marks in degree, its living entirely another kind of life to learn to program well. Its totally another kind of living when your instincts will tell you how much important the ANSI C standard is, how much useful it is to write simple and small program stimulating your ability to undertstand basic algorithms and data structures. A majority of programmers in India (and thats quite of a majority) focus on short-term programming results, like writing a software in 1 month and tetsing it for 3 months while real programmers will write it in 1 or 2 months and testing will almost take care of itself in the end. Correctly written programs have 6 times lesser duration of testing as compared to badly written ones (without any regard for algorithms and data structures).  I even say that corporation must hire 10 very good programmers rather than 100 crappy ones, 10 will produce code in half of the time than those 100 and testing will not be as frustrating to project manager as what those 100 will do.  These 10 crazy and mad guys will do corrections and put their attitude and mentality into the code while the crappy ones will only add bugs, where fixing one bug will produce another one. A typical programmer in Indian corporate tries to avoid reference manuals and browsing through mailing list discussions for hours and days and defintely avoids doing his home work before asking. All of these lead to softwares with lots of bugs and wastage of time and energy of both corporate and the the new programmer who reads the code later to modify or add to that code.  They think its time consuming, it gives a headache and its not important to learn programming as a way of life as compared to project-deadline. They are wrong. Their focus is short term while the real programming is long term. No pain, no gain and pain becomes a joy in long term.  The programmers/managers who say that you can&#8217;t write programs in small time using the real Hacker&#8217;s way are like the persons who say you can&#8217;t make money by giving source for free. In fact you can. When your instincts and feelings for programming are dead, the real programmer inside you is dead.
</p>
<p align="justify">
Wrote this small piece of code to get data form any web-site and save it in the program for later use. This is what I wrote in 1 hour hack, a program that compiles fine with <strong>-ansi -pedantic -Wall -Wextra</strong> flags to <strong>gcc</strong> and sill buggy:<br />
<pre class="brush: cpp;">
/* WARNING: Buggy Program. Not using libcurl in a proper way. Not recommended to be even saved on HDD 
 *
 */
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;curl/curl.h&gt;

size_t save_data(void* buffer, size_t size, size_t nmemb, void* incoming_data);

enum {
  SIZE_ERROR_ARR = CURL_ERROR_SIZE * 4
};


int main(void)
{
  CURL* curl;
  CURLcode res;
  char error_arr[SIZE_ERROR_ARR+1] = {0};

  curl = curl_easy_init();
  if(curl)
    {
      /* Use one VERSION by commenting the other */

      /* VERSION 1 : Outputs to stdout*/ 
      curl_easy_setopt(curl, CURLOPT_URL, &quot;http://google.com&quot;);
      res = curl_easy_perform(curl); 

      /* VERSION 2 : Saves to array 
      curl_easy_setopt(curl, CURLOPT_URL, &quot;http://google.com&quot;);
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_data);
      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_arr);
      res = curl_easy_perform(curl); 
      */
      if(res)
	{
	  printf(&quot;IN: %s @%d: Some error occured while curl_easy_perform(), ERRORBUFFER said: \n&quot;, __FILE__, __LINE__);
	  printf(&quot;%s\n&quot;, error_arr);
	}

      /* always cleanup */
      curl_easy_cleanup(curl);
    }

  return 0;
}



size_t save_data(void* buffer, size_t size, size_t nmemb, void* incoming_data)
{
  size_t idx;
  const size_t END_INDEX = size * nmemb;
  char* src = (char*) buffer;

  char* dest = malloc(END_INDEX * sizeof*dest);
  memset(dest, '&#092;&#048;', END_INDEX);

  if(NULL == dest)
    {
      printf(&quot;IN: %s @%d: Out of Memory\n&quot;, __FILE__, __LINE__);
      exit(EXIT_FAILURE);
    }

  /*  Using Static Array by taking extremely large size wasting 100 times the memoery required.
      char dest[100001] = {0};
      How to know the size of array in advance. 
      char dest[END_INDEX + 1] = {0}; will not work as variable-size arrays are not allowed in ANSI standard. But using variable-size 
      is good solution to the problem ???
      malloc() will be expensive for 10 million http sends/receive  waitings to happen. 
      So what to do ???
  */

  for(idx = 0; idx &lt; END_INDEX - 1; ++idx)
    {
      dest[idx] = *src++;
    }
  dest[idx] = '&#092;&#048;';
  printf(&quot;--------------------------------------\n&quot;);
  /* I wonder what 4th argument is used for when it contains garbage :-o */
  printf(&quot;incoming_data = %s\n&quot;, (char*)incoming_data); 
  printf(&quot;dest = %s\n&quot;, dest);

  if(0 == strcmp(dest, &quot;&quot;))
    {
      printf(&quot;EMPTY string received\n&quot;);
    }

  printf(&quot;--------------------------------------\n&quot;);
  return END_INDEX;
}
</pre>
</p>
<p align="justify">
Unfortunately, this is exactly what you can expect from a guy with no gifted ability to program, who learns from hard-work and experience only. I learn by dwelling deep into refernce manuals, man pages, FAQs and mailing-list archives and thats tough and the only one way to learn to use a program in proper way, the way the API was supposed to be used.  I discussed this on mailing list and I get this reply from the <a href="http://daniel.haxx.se/">creator of cURL</a>: &#8220;<em>See http://curl.haxx.se/libcurl/c/getinmemory.html</em>&#8220;,  one line reply which matches exactly with what I needed. Any programmer who does his <a href="http://catb.org/~esr/faqs/smart-questions.html#before">home work</a> before asking questions  deserves such kind of meaningful  reply <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Here is the whole conversation: <a href="http://curl.haxx.se/mail/lib-2010-11/0123.html">http://curl.haxx.se/mail/lib-2010-11/0123.html</a>  and here is the code I needed to understand before aksing anymore questions:</p>
<p><pre class="brush: cpp;">
/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ &lt;| |___
 *                             \___|\___/|_| \_\_____|
 *
 *
 * Example source code to show how the callback function can be used to
 * download data into a chunk of memory instead of storing it in a file.
 */

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#include &lt;curl/curl.h&gt;

struct MemoryStruct {
  char *memory;
  size_t size;
};


static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  mem-&gt;memory = realloc(mem-&gt;memory, mem-&gt;size + realsize + 1);
  if (mem-&gt;memory == NULL) {
    /* out of memory! */
    printf(&quot;not enough memory (realloc returned NULL)\n&quot;);
    exit(EXIT_FAILURE);
  }

  memcpy(&amp;(mem-&gt;memory[mem-&gt;size]), ptr, realsize);
  mem-&gt;size += realsize;
  mem-&gt;memory[mem-&gt;size] = 0;

  return realsize;
}


int main(int argc, char **argv)
{
  CURL *curl_handle;

  struct MemoryStruct chunk;

  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
  chunk.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */
  curl_handle = curl_easy_init();

  /* specify URL to get */
  curl_easy_setopt(curl_handle, CURLOPT_URL, &quot;http://www.example.com/&quot;);

  /* send all data to this function  */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

  /* we pass our 'chunk' struct to the callback function */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&amp;chunk);

  /* some servers don't like requests that are made without a user-agent
     field, so we provide one */
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, &quot;libcurl-agent/1.0&quot;);

  /* get it! */
  curl_easy_perform(curl_handle);

  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);

  /*
   * Now, our chunk.memory points to a memory block that is chunk.size
   * bytes big and contains the remote file.
   *
   * Do something nice with it!
   *
   * You should be aware of the fact that at this point we might have an
   * allocated data block, and nothing has yet deallocated that data. So when
   * you're done with it, you should free() it as a nice application.
   */

  printf(&quot;%lu bytes retrieved\n&quot;, chunk.size);

  if(chunk.memory)
    free(chunk.memory);

  /* we're done with libcurl, so clean it up */
  curl_global_cleanup();

  return 0;
}
</pre></p>
<p align="justify">
So I came to know that CURLOPT_WRITEFUNCTION sets the function that libcurl will call. CURLOPT_WRITEDATA is used to set the pointer that the function gets in its fourth argument. They were both needed to use libcurl properly, the way it was supposed to be used. Daniel was very faaaast in replying and to the point. Thanks to him, he saved a lot of time and energy of mine.
</p>
<p align="justify"> &nbsp; </p>
<p align="justify"> &nbsp; </p>
<p align="justify">
<em><br />
Copyright © 2010 Arnuld Uttre, #331/type-2/sector-1, Naya Nangal, Distt. &#8211; Ropar, Punjab (INDIA) &#8211; 140126</p>
<p>Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.<br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lispmachine.wordpress.com/440/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lispmachine.wordpress.com/440/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lispmachine.wordpress.com/440/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lispmachine.wordpress.com&amp;blog=1624923&amp;post=440&amp;subd=lispmachine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lispmachine.wordpress.com/2010/11/15/440/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f45bcf34b0e8389c9c6aaac3249f9e4d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">arnuld</media:title>
		</media:content>
	</item>
	</channel>
</rss>
