<?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>[ Code Explored ]</title>
	<atom:link href="http://born2c0de.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://born2c0de.wordpress.com</link>
	<description>Demystify Code Internals, Standards and more...</description>
	<lastBuildDate>Fri, 16 Jan 2009 07:23:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='born2c0de.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>[ Code Explored ]</title>
		<link>http://born2c0de.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://born2c0de.wordpress.com/osd.xml" title="[ Code Explored ]" />
	<atom:link rel='hub' href='http://born2c0de.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Blog Discontinued</title>
		<link>http://born2c0de.wordpress.com/2009/01/16/blog-discontinued/</link>
		<comments>http://born2c0de.wordpress.com/2009/01/16/blog-discontinued/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 07:23:05 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Blog Updates]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/?p=16</guid>
		<description><![CDATA[Hi Guys, I shall be maintaining a blog at www.sanchitkarve.com/blog from now on instead of out here. So keep visiting there for newer posts.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=16&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi Guys,</p>
<p>I shall be maintaining a blog at <a title="New Blog Address" href="http://www.sanchitkarve.com/blog" target="_blank">www.sanchitkarve.com/blog</a> from now on instead of out here.</p>
<p>So keep visiting there for newer posts.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=16&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2009/01/16/blog-discontinued/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Applications of One way Hash Functions in Security</title>
		<link>http://born2c0de.wordpress.com/2007/08/19/0xa-applications-of-one-way-hash-functions-in-security/</link>
		<comments>http://born2c0de.wordpress.com/2007/08/19/0xa-applications-of-one-way-hash-functions-in-security/#comments</comments>
		<pubDate>Sun, 19 Aug 2007 12:09:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[I/O : Interesting Observations]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2007/08/19/0xa-applications-of-one-way-hash-functions-in-security/</guid>
		<description><![CDATA[We&#8217;ve all heard of one-way Hash Functions sometime or the other. Most of us have heard about them from books. An Algorithm&#8217;s explanation is usually followed by its applications and most books mention only one major application (in security) ie. implementing password checks. I used to wonder:&#8221;That&#8217;s it? One Application in Security?&#8221;, and searching on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=12&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all heard of one-way Hash Functions sometime or the other.<br />
Most of us have heard about them from books.<br />
An Algorithm&#8217;s explanation is usually followed by its applications and most books mention only one major application (in security) ie. implementing password checks.</p>
<p>I used to wonder:&#8221;That&#8217;s it? One Application in Security?&#8221;, and searching on the internet (and some more books) didn&#8217;t help either.</p>
<p>Luckily, I&#8217;ve finally found my answers. Now, I&#8217;m able to appreciate one-way Hash functions a lot more because I&#8217;ve seen it in action. If I had read this application in a book I&#8217;m sure that I wouldn&#8217;t have realized its importance.</p>
<p>A few days ago, Nokia Corporation issued a notice to all customers that some of its BL-5C Model Batteries had some manufacturing defects which could cause it to explode. It asked customers to check if their batteries were manufactured between December 2005 and November 2006 and if so, get the battery replaced for free.</p>
<p>Nokia also allowed its customers to type in their 26 Character Battery Code on their website (<a href="http://www.nokia.com/batteryreplacement">www.nokia.com/batteryreplacement</a>) to see if their battery was faulty or not.</p>
<p>I decided to check the script which finds out when the battery was manufactured. I thought that by looking at the source code, I could figure out exactly which batteries were faulty.</p>
<p>This is what the script looks like:</p>
<blockquote><p><span style="font-family:&quot;color:#3366ff;">function rcrcheck_serial() {<br />
<strong><span style="color:#ff0000;">var isgood=false; </span></strong><br />
<strong><span style="color:#ff0000;">var serial=document.enterserial.serial.value;<br />
</span></strong>var a=&#8221;;<br />
var b=&#8221;;<br />
var c=&#8221;;<br />
if(serial.length&lt;26) {<br />
alert(&#8220;The identification number is incorrect. Please check that you have entered the full 26 characters of the battery identification number.&#8221;);<br />
return false;<br />
}<br />
if(serial.length&gt;26) {<br />
alert(&#8220;The identification number is incorrect. Please check that you have entered the full 26 characters of the battery identification number.&#8221;);<br />
return false;<br />
}<br />
<span style="color:#ff0000;"><strong>a=md5(serial.substr(7,6));<br />
b=md5(serial.substr(13,1));<br />
c=md5(serial.substr(14,3)); </strong></span><br />
//###<br />
if(a!=&#8221;ea4a302b5cbd017871ec94fd6ae189b5&#8243; //###<br />
&amp;&amp;amp;amp;amp;amp; a!=&#8221;1f098214896cc40cfabc3b2403a65b75&#8243; //###<br />
&amp;&amp; a!=&#8221;fd06cd296b4bf634d85e26884565aa6c&#8221;) { //###<br />
window.location=&#8221;rcrb2.html&#8221;;<br />
return false;<br />
}if(b==&#8221;8d9c307cb7f3c4a32822a51922d1ceaa&#8221; b==&#8221;7b8b965ad4bca0e41ab51de7b31363a1&#8243;) { //###<br />
if(c==&#8221;84eb13cfed01764d9c401219faa56d53&#8243;){return true;} //###<br />
if(c==&#8221;d2490f048dc3b77a457e3e450ab4eb38&#8243;){return true;} //###<br />
if(c==&#8221;441954d29ad2a375cef8ea524a2c7e73&#8243;){return true;} //###<br />
if(c==&#8221;0e51011a4c4891e5c01c12d85c4dcaa7&#8243;){return true;} //###<br />
if(c==&#8221;af032fbcb07ffc7bd2569d86ae4ce1f5&#8243;){return true;} //###<br />
if(c==&#8221;73f7634ab3f381fb40995f93740b3f8a&#8221;){return true;} //###<br />
if(c==&#8221;738cccd4fda172441f216712a488dca6&#8243;){return true;} //###<br />
if(c==&#8221;f803dfeb3583d5099a58a7478f28bd75&#8243;){return true;} //###<br />
if(c==&#8221;7f5144f962efde75e0f7661e032166db&#8221;){return true;} //###<br />
if(c==&#8221;8fc4c7ab4453d247e011738197b6136c&#8221;){return true;} //###</p>
<p></span></p>
<p><span style="font-family:&quot;color:#ff0000;"><strong>/* Some more Comparisons */</strong></span></p>
<p><span style="font-family:&quot;color:#3366ff;"><br />
if(c==&#8221;defd40204344c9659a0a3eb4ebc125f6&#8243;){return true;} //###<br />
if(c==&#8221;c4de9fe96832a877668d0dced80657b8&#8243;){return true;} //###<br />
if(c==&#8221;2c62105ee18ecd5f0ee37bc8c35718eb&#8221;){return true;} //###<br />
if(c==&#8221;3994f23bfb2b89994bd6e828977b42ae&#8221;){return true;} //###<br />
if(c==&#8221;28fd0fbd334515deb8a8291b71941c9e&#8221;){return true;} //###<br />
if(c==&#8221;9ac05befca7d6499e3abec9bdfef2b68&#8243;){return true;} //###<br />
if(c==&#8221;1732cb437260c60a0744aea8aedfa331&#8243;){return true;} //###<br />
if(c==&#8221;e1eee5e2b42d45443cdc82db1a3bc465&#8243;){return true;} //###<br />
if(c==&#8221;7d06a9cf10f2e9e47e77d6c6cfaa7f54&#8243;){return true;} //###<br />
if(c==&#8221;2618045a3a5fc883e65b6bec2fcac3c8&#8243;){return true;} //###<br />
if(c==&#8221;2421fcb1263b9530df88f7f002e78ea5&#8243;){return true;} //###<br />
if(c==&#8221;fccb60fb512d13df5083790d64c4d5dd&#8221;){return true;} //###<br />
if(c==&#8221;15d4e891d784977cacbfcbb00c48f133&#8243;){return true;} //###<br />
if(c==&#8221;c203d8a151612acf12457e4d67635a95&#8243;){return true;} //###<br />
if(c==&#8221;13f3cf8c531952d72e5847c4183e6910&#8243;){return true;} //###<br />
if(c==&#8221;550a141f12de6341fba65b0ad0433500&#8243;){return true;} //###<br />
if(c==&#8221;67f7fb873eaf29526a11a9b7ac33bfac&#8221;){return true;} //###<br />
if(c==&#8221;1a5b1e4daae265b790965a275b53ae50&#8243;){return true;} //###<br />
if(c==&#8221;9a96876e2f8f3dc4f3cf45f02c61c0c1&#8243;){return true;} //###<br />
if(c==&#8221;941e1aaaba585b952b62c14a3a175a61&#8243;){return true;} //###<br />
if(c==&#8221;9431c87f273e507e6040fcb07dcb4509&#8243;){return true;} //###<br />
if(c==&#8221;49ae49a23f67c759bf4fc791ba842aa2&#8243;){return true;} //###<br />
if(c==&#8221;e44fea3bec53bcea3b7513ccef5857ac&#8221;){return true;} //###<br />
if(c==&#8221;821fa74b50ba3f7cba1e6c53e8fa6845&#8243;){return true;} //###<br />
if(c==&#8221;250cf8b51c773f3f8dc8b4be867a9a02&#8243;){return true;} //###<br />
if(c==&#8221;42998cf32d552343bc8e460416382dca&#8221;){return true;} //###<br />
if(c==&#8221;0353ab4cbed5beae847a7ff6e220b5cf&#8221;){return true;} //###<br />
if(c==&#8221;51d92be1c60d1db1d2e5e7a07da55b26&#8243;){return true;} //###<br />
if(c==&#8221;428fca9bc1921c25c5121f9da7815cde&#8221;){return true;} //###<br />
if(c==&#8221;f1b6f2857fb6d44dd73c7041e0aa0f19&#8243;){return true;} //###<br />
if(c==&#8221;68ce199ec2c5517597ce0a4d89620f55&#8243;){return true;} //###<br />
if(c==&#8221;e836d813fd184325132fca8edcdfb40e&#8221;){return true;} //###<br />
if(c==&#8221;ab817c9349cf9c4f6877e1894a1faa00&#8243;){return true;} //###<br />
if(c==&#8221;8e6b42f1644ecb1327dc03ab345e618b&#8221;){return true;} //###<br />
if(c==&#8221;ef575e8837d065a1683c022d2077d342&#8243;){return true;} //###<br />
if(c==&#8221;2050e03ca119580f74cca14cc6e97462&#8243;){return true;} //###<br />
if(c==&#8221;25ddc0f8c9d3e22e03d3076f98d83cb2&#8243;){return true;} //###<br />
if(c==&#8221;5ef0b4eba35ab2d6180b0bca7e46b6f9&#8243;){return true;} //###<br />
if(c==&#8221;598b3e71ec378bd83e0a727608b5db01&#8243;){return true;} //###<br />
if(c==&#8221;74071a673307ca7459bcf75fbd024e09&#8243;){return true;} //###<br />
}if(b==&#8221;69691c7bdcc3ce6d5d8a1361f22d04ac&#8221; b==&#8221;6f8f57715090da2632453988d9a1501b&#8221;) { //###<br />
if(c==&#8221;2bb232c0b13c774965ef8558f0fbd615&#8243;) {return true;} //###<br />
if(c==&#8221;ba2fd310dcaa8781a9a652a31baf3c68&#8243;) {return true;} //###<br />
if(c==&#8221;69421f032498c97020180038fddb8e24&#8243;) {return true;} //###<br />
if(c==&#8221;85422afb467e9456013a2a51d4dff702&#8243;) {return true;} //###<br />
if(c==&#8221;13f320e7b5ead1024ac95c3b208610db&#8221;) {return true;} //###<br />
}<br />
window.location=&#8221;rcrb2.html&#8221;;<br />
return false;<br />
}</p>
<p></span></p></blockquote>
<p>This code is awesome because, even after reading the source code, nobody can figure out which models are faulty. At the most what we can understand is that Battery Information (Date of Manufacture, Location of Manufacture) is present between the 8th and 17th characters. The characters between 18 and 26th positions could hold the amount of batteries manufactured by the factory before the current unit. We also know that the battery is faulty for some 334 combinations of characters between the 14th and 17th positions. But having this knowledge is futile.</p>
<p>Hence by using a one-way Hash Algorithm (MD5 in this case), we can hide such information (factory codes of factories which manufactured the faulty batteries) even in the source code. This way we can protect such vital information from being stolen by anyone even if he has access to the complete source code, and this according to me is one of the most brilliant applications of One-way Hash functions.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=12&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2007/08/19/0xa-applications-of-one-way-hash-functions-in-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>0&#215;9 : Reversing a String&#8230;Recursively</title>
		<link>http://born2c0de.wordpress.com/2006/10/22/0x9-reversing-a-stringrecursively/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/22/0x9-reversing-a-stringrecursively/#comments</comments>
		<pubDate>Sun, 22 Oct 2006 06:45:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/22/0x9-reversing-a-stringrecursively/</guid>
		<description><![CDATA[When you&#8217;re asked to reverse a string, you&#8217;ll mostly use the strrev() function or write your own boring implementation using loops. Ever tried it recursively?   Have a look at this:   #include void ret_str(char* s){     if(*s != &#8221;)     ret_str(s+1);     cout&#60;&#60;*s;} int main(){   ret_str(&#8220;born2c0de&#8221;);   return 0;} Isn&#8217;t that some neat piece of code? All we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=11&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><span style="font-family:times new roman,serif;font-size:130%;">When you&#8217;re asked to reverse a string, you&#8217;ll mostly use the strrev() function or write your own boring implementation using loops.</span></div>
<div><span style="font-family:times new roman,serif;font-size:130%;">Ever tried it recursively?</span></div>
<div><span style="font-family:times new roman,serif;font-size:130%;"></span> </div>
<div><span style="font-family:times new roman,serif;font-size:130%;">Have a look at this:</span></div>
<div><span style="font-family:times new roman,serif;font-size:130%;"></span> </div>
<div>
<p><span style="font-family:courier new,monospace;font-size:85%;color:#3366ff;">#include <br /></span>
<p><span style="font-family:courier new,monospace;font-size:85%;color:#3366ff;">void ret_str(char* s)<br />{</span></p>
<p><span style="font-family:courier new,monospace;font-size:85%;color:#3366ff;">    if(*s != &#8221;)<br />     ret_str(s+1);</span></p>
<p><span style="font-family:courier new,monospace;font-size:85%;color:#3366ff;">    cout&lt;&lt;*s;<br />}</span></p>
<p><span style="font-family:courier new,monospace;font-size:85%;color:#3366ff;">int main()<br />{<br />   ret_str(&#8220;born2c0de&#8221;);<br />   return 0;<br />}</span></p>
<p><span style="font-family:times new roman,serif;font-size:130%;">Isn&#8217;t that some neat piece of code? All we have to do is push the next character to the stack, so when the stack is popped, the characters come out in reverse order.</span></p>
<p><span style="font-family:Times New Roman;font-size:130%;">I&#8217;m not saying that this functions is efficient, it&#8217;s horribly slow and sluggish and larger strings will result in overloading the stack.</span></p>
<p><span style="color:#ffcc00;"><span style="font-family:Times New Roman;font-size:130%;">But when writing any function, try to write a recursive version of it to test your skills. It&#8217;s easy and I mention how it can be done in my next tutorial on </span></span><span style="font-family:Times New Roman;font-size:130%;"><strong><span style="color:#ff0000;">Recursion</span></strong></span><span style="color:#ffcc00;"><span style="font-family:Times New Roman;font-size:130%;"><strong> </strong></span><span style="font-family:Times New Roman;font-size:130%;">which will be out by</span><span style="font-family:Times New Roman;font-size:130%;"> <strong>January 2007.</strong></span></span><span style="font-family:Times New Roman;font-size:130%;"><span style="color:#ff0000;"><strong> </strong></span><span style="font-size:85%;color:#cc33cc;">(Thanks to my college schedule)</span></span></p>
<p> </p>
<p> </p>
<p align="center"><span><strong>- Sanchit Karve</strong></span><br /><strong><span>[ b o r n 2 c 0 d e ]</span></strong><br /><strong><span>printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div><b><span style="font-size:78%;color:#ffffff;">[EOF]</span></b></div>
</div>
<p>i{content: normal !important}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=11&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/22/0x9-reversing-a-stringrecursively/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>String Termination in C</title>
		<link>http://born2c0de.wordpress.com/2006/10/14/0x8-string-termination-in-c/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/14/0x8-string-termination-in-c/#comments</comments>
		<pubDate>Sat, 14 Oct 2006 10:53:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/14/0x8-string-termination-in-c/</guid>
		<description><![CDATA[This is a typical assignment given to students who are learning C/C++. &#8220;Write a function that copies the contents of one string into another.&#8221; I&#8217;d like to first point out that by string, a character array is referred to, and not an object of the string class. Majority of the class came up with this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=10&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><span style="font-family:&quot;font-size:medium;">This is a typical assignment given to students who are learning C/C++.</span></div>
<div></div>
<div><span style="font-family:&quot;font-size:medium;">&#8220;Write a function that copies the contents of one string into another.&#8221;</span></div>
<div><span style="font-family:&quot;color:#ffcc00;font-size:medium;">I&#8217;d like to first point out that by string, a character array is referred to, and not an object of the string class.</span></div>
<div></div>
<div><span style="font-family:&quot;font-size:medium;">Majority of the class came up with this code:</span></div>
<div></div>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">#include &lt;stdio.h&gt;</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">void copy(char [],char []);</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">void main()<br />
{<br />
char s1[10],s2[10];<br />
printf(&#8220;Enter String 1:&#8221;);<br />
gets(s1);<br />
copy(s1,s2);<br />
printf(&#8220;\nThe copied string is: &#8220;);<br />
puts(s2);<br />
}</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">void copy(char x[],char y[])<br />
{<br />
int i=0;<br />
while(x[i])<br />
{<br />
y[i]=x[i];<br />
i++;<br />
}<br />
}</span></p>
<p><span style="font-family:&quot;font-size:medium;">Can you notice that something is missing in the copy() function? Yes, the null termination character &#8221; was not appended at the end of the new string. Luckily, another student in my class pointed out this error to everyone. But then, the rest of the students asked that  <span style="color:#ffcc00;">if their code was wrong, then how come it works for most of the strings?</span></span></p>
<p><span style="font-family:&quot;font-size:medium;">To this he had no answer. And I had better things to do (such as sleep) than to explain these guys why it happens.</span></p>
<p><span style="font-family:&quot;font-size:medium;">Now (since I&#8217;m awake) I&#8217;ll put forth a fitting explanation.</span></p>
<p><span style="font-family:&quot;font-size:medium;">Observe the declaration of this character array:</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">char name[10]=&#8221;Sanchit&#8221;;</span></p>
<p><span style="font-family:&quot;font-size:medium;">Internally, this String is stored like this:</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">S,a,n,c,h,i,t,0,0,0</span></p>
<p><span style="font-family:&quot;font-size:medium;">Yes, if the string initial declaration is less than the size provided in the square brackets, the rest of the elements are filled with zeros. This is applicable for arrays of any data type. </span></p>
<p><span style="font-family:&quot;font-size:medium;">Since the ASCII Value of &#8221; is 0, I can represent my array like this again,</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">S,a,n,c,h,i,t,,,</span></p>
<p><span style="font-family:&quot;font-size:medium;">Doesn&#8217;t this give you a hint?</span></p>
<p><span style="font-family:&quot;font-size:medium;">If you create another array of the same size (here: 10) and copy this string to it using the code given above, the rest of the elements after the String are already &#8221;. So there is no need to append the &#8221; character as C would know where the string terminates. </span></p>
<p><span style="font-family:&quot;font-size:medium;">But what if the String is larger? Assuming &#8220;Sanchit Karve&#8221; is stored in the same Array, it will look like this:</span></p>
<p><span style="font-family:&quot;color:#3366ff;font-size:x-small;">S,a,n,c,h,i,t,&#8217; &#8216;,K,a,r,v,e, &lt;garbage&gt; , &lt;garbage&gt; , &#8230;</span></p>
<p><span style="font-family:&quot;color:#ffcc00;font-size:medium;">If you notice, that inspite of the string being larger than the array size, it is stored completely after occupying the space after the array. Since, now the space outside the array has been accessed, the data out there is not zero. Instead they contain garbage values. So, for such situations, we need to append the &#8221; character at the end of the string, so that C can figure out where the string ends. Otherwise, C will output all bytes till a zero is encountered. </span></p>
<p><span style="font-family:&quot;font-size:medium;">So, appending the &#8221; character is not required in theory, because we assume that the string length is lesser than the array size. We should also ensure that we declare enough space before accepting such input. But still, we must do this (append ) as a precaution because of the following reasons: </span></p>
<ol>
<li><span style="font-family:&quot;color:#ff0000;font-size:medium;">Unlike other Languages C/C++ do not provide Array Bound Checking features.</span></li>
<li><span style="font-family:&quot;color:#ff0000;font-size:medium;">Hackers make the most of such types of errors, known as <strong>Buffer Overflow</strong> Errors, and launch <strong>Buffer Overflow Attacks</strong> where the string is inserted with shellcode (yes, in hex), and the return address of the function is overwritten to the address of the string. This results in the processor passing control to the shellcode and executing it after the function returns. </span></li>
</ol>
<div><span style="font-family:&quot;font-size:medium;">I have been studying Exploits for quite some time now, especially the Buffer Overflow attack. It is an interesting field of study.</span></div>
<div><span style="font-family:&quot;font-size:medium;">But just because we append the &#8221; character does not mean that Buffer Overflow Errors won&#8217;t occur. But it&#8217;s done just as a safety measure so that the program can run without any faults. </span></div>
<div></div>
<div><span style="font-family:&quot;color:#cc33cc;font-size:medium;">Speaking of Exploits, I&#8217;d like to add that you will see posts about them and a lot more interesting algorithms and standards soon.</span></div>
<div></div>
<div>
<p align="center"><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="color:#ffffff;font-size:xx-small;">[EOF]</span></strong></div>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=10&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/14/0x8-string-termination-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Passing 2D Arrays to Functions</title>
		<link>http://born2c0de.wordpress.com/2006/10/14/0x7-passing-2d-arrays-to-functions/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/14/0x7-passing-2d-arrays-to-functions/#comments</comments>
		<pubDate>Sat, 14 Oct 2006 10:16:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/14/0x7-passing-2d-arrays-to-functions/</guid>
		<description><![CDATA[This is an extremely common error as far as passing 2D Arrays to functions goes. A common function prototype which receives a 2D Array would look like this: void func1(int [][]); Actually, as normal as it may seem, it is incorrect. For passing 2D Arrays to a function, the column element must be mentioned like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=9&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><span style="font-family:&quot;font-size:medium;">This is an extremely common error as far as passing 2D Arrays to functions goes.</span></div>
<div></div>
<div><span style="font-family:&quot;font-size:medium;">A common function prototype which receives a 2D Array would look like this:</span></div>
<div><span style="font-family:&quot;color:#3333ff;font-size:x-small;">void func1(int [][]);</span></div>
<div><span style="font-family:&quot;font-size:medium;">Actually, as normal as it may seem, it is incorrect.<br />
For passing 2D Arrays to a function, the column element must be mentioned like this:<br />
</span></div>
<div><span style="font-family:&quot;"><span style="font-size:x-small;"><span style="color:#3333ff;">void func1(int [][2]); </span><span style="color:#cc33cc;">// 2 can be any number</span></span></span></div>
<p><span style="font-family:&quot;font-size:medium;">Why is it like this? If C/C++ compilers can figure out the size of a 1D array then why not for 2D?</span></p>
<p><span style="font-family:&quot;font-size:medium;">This is because 2D Array elements are stored consecutively as two 1D Arrays.</span></p>
<p><span style="font-family:&quot;font-size:medium;">Consider this code.<br />
</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">#include &lt;stdio.h&gt; </span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">int main()<br />
{<br />
int x[][2]={1,2,3,4,5,6,7,8,9,10};<br />
int *p,*q;<br />
int i;<br />
p=&amp;x[0][0];<br />
for(i=0;i&lt;10;i++)<br />
printf(&#8220;%d &#8220;,*(p+i)); </span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> printf(&#8220;\n\n Now using pointer q:\n&#8221;);</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> q=&amp;x[1][0];<br />
for(i=0;i&lt;10;i++)<br />
printf(&#8220;%d &#8220;,*(q+i));</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> return 0;<br />
}</span></p>
<p><span style="font-family:&quot;"><span style="font-size:medium;">Run the Program. You&#8217;ll get this as the output.<br />
</span></span><span style="font-family:&quot;color:#3333ff;font-size:x-small;">1 2 3 4 5 6 7 8 9 10</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">Now using pointer q:<br />
3 4 5 6 7 8 9 10 0 4239532</span></p>
<div><span style="font-family:&quot;font-size:medium;">Now change the number 2 in the array declaration to 5 like this:<br />
</span></div>
<div><span style="font-family:&quot;color:#3333ff;font-size:x-small;">int x[][5]={1,2,3,4,5,6,7,8,9,10};</span></div>
<p><span style="font-family:&quot;"><span style="font-size:medium;">Run the code and observe the output:<br />
</span></span><span style="font-family:&quot;color:#3333ff;font-size:x-small;">1 2 3 4 5 6 7 8 9 10</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">Now using pointer q:<br />
6 7 8 9 10 0 4239532 0 4235541 1</span></p>
<p><span style="font-family:&quot;font-size:medium;">As you can see, the output using the pointer p remains unchanged because we&#8217;re setting it to the first element of the array itself, so it iterates to every element after it. </span></p>
<p><span style="font-family:&quot;color:#ff0000;font-size:medium;">But q is set to the first element of the 2nd row.<br />
Now which one is the First element of the second row?<br />
It is 6. How do we know it?<br />
It&#8217;s because we&#8217;ve placed 5 in the column bracket to specify the bounds of each column. </span></p>
<p><span style="font-family:&quot;color:#ff0000;font-size:medium;">So if we don&#8217;t place any value, the compiler gets confused as to how many numbers can be grouped into one column.<br />
And whenever such confusion occurs, the compiler generates the error Size of the int[] is unknown or zero </span></p>
<p><span style="font-family:&quot;font-size:medium;">I hope you&#8217;ve realised why the column parameter is required.</span></p>
<p><span style="font-family:&quot;"><span style="font-size:medium;"><span style="color:#ffcc00;">And now it&#8217;s time to tell you guys why I posted this today.<br />
We were given an assignment to pass a 2D Matrix to a function and find the sum.<br />
Nobody got the solution and I had to write the solution on the blackboard and let the rest of the class take it down.<br />
And I used this function declaration:<br />
</span><br />
</span><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> void add_mat(int [][],int [][],int [][]);</span></span></p>
<p><span style="font-family:&quot;color:#ffcc00;font-size:medium;">No student realised my mistake, neither did the professor ( and neither did I )</span></p>
<p><span style="font-family:&quot;color:#ffcc00;font-size:medium;">I realised what I had done after I was taking the train back home and was just thinking about how my day had passed.<br />
Now everyone has copied down wrong code into their notebooks.<br />
I&#8217;ve gotta tell them to correct the code tomorrow.</span></p>
<div><span style="font-family:&quot;color:#ffcc00;font-size:medium;">It&#8217;s Ironical that a guy who is explaning why something is incorrect, was the same guy who told the rest of the class today that passing the column value is optional while passing a 2D array to a function. </span></div>
<div></div>
<div><span style="font-family:&quot;color:#ffcc00;font-size:medium;">It&#8217;s probably because of the amount of time I sleep during lectures. And unfortunately, no matter how hard I try to stay awake, my professors always seem to prove that they&#8217;re the best doctors for insomniac patients. </span></div>
<div></div>
<div><span style="font-family:&quot;color:#ffcc00;font-size:medium;"><span style="font-size:x-small;"> </span></p>
<p align="center"><span style="font-size:x-small;"><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;"> printf(&#8220;I&#8217;m a %XR&#8221;,195936478);</span></strong></span></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="color:#ffffff;font-size:x-small;">[EOF]</span></strong></div>
<p></span></div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=9&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/14/0x7-passing-2d-arrays-to-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Datatype Range Violation Consequences</title>
		<link>http://born2c0de.wordpress.com/2006/10/14/0x6-datatype-range-violation-consequences/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/14/0x6-datatype-range-violation-consequences/#comments</comments>
		<pubDate>Sat, 14 Oct 2006 09:55:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/14/0x6-datatype-range-violation-consequences/</guid>
		<description><![CDATA[A lot of coders wonder why a number stored in a variable, if exceeds it&#8217;s range, turns into a wierder negative number. To explain this phenomena they offer a conclusion without showing any evidence and this conclusion is in no way close to the truth. Here&#8217;s what happens. The numbers change because of the complement notation that the computer [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=8&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><span style="font-family:&quot;font-size:medium;">A lot of coders wonder why a number stored in a variable, if exceeds it&#8217;s range, turns into a wierder negative number.</span></div>
<div><span style="font-family:&quot;font-size:medium;">To explain this phenomena they offer a conclusion without showing any evidence and this conclusion is in no way close to the truth.</span></div>
<div><span style="font-family:&quot;font-size:medium;">Here&#8217;s what happens.</span></div>
<p><span style="font-family:&quot;font-size:medium;">The numbers change because of the complement notation that the computer uses to store negative numbers.</span></p>
<p><span style="font-family:&quot;font-size:medium;">And it&#8217;s not the compiler&#8217;s fault but the processor&#8217;s limitation.<br />
This code will give negative numbers on 32-bit,16-bit and 8bit Microprocessors as each register can hold 32/16/8 bits respectively.<br />
You won&#8217;t encounter this problem on 64-bit processors as each register can hold a maximum of 64-bits. (Actually, you will encounter the same problem since the value of INT_MAX will be a 64-bit number for a 64 bit processor&#8230;.but It&#8217;ll work fine with 32-bit numbers) </span></p>
<p><span style="font-family:&quot;font-size:medium;">Adding a Number to a maximum value set in these registers creates the problem.<br />
Have a look at this.</span></p>
<p><span style="font-family:&quot;font-size:medium;">Since INT_MAX is the largest 32-bit number that<br />
Here&#8217;s the code it generates for this C++ code:<br />
I&#8217;ve typecasted it just so that my compiler compiles it.<br />
</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">printf(&#8220;%d = %x\n%d = %x&#8221;,INT_MAX,INT_MAX,(unsigned long)INT_MAX+1,(unsigned long)INT_MAX+1);</span></p>
<div><span style="font-family:&quot;font-size:medium;">The compiler generates the following code:<br />
</span></div>
<div><span style="color:#3333ff;"><span style="font-family:&quot;font-size:x-small;">push    10000000000000000000000000000000b<br />
push    10000000000000000000000000000000b<br />
push    1111111111111111111111111111111b<br />
push    1111111111111111111111111111111b<br />
push    offset aDXDX </span><span style="font-size:x-small;"><span style="font-family:&quot;"><span style="color:#cc33cc;">; format<br />
</span>call    _printf<br />
add     esp, 14h</span></span></span></div>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">&#8230;<br />
&#8230;<br />
aDXDX  db &#8216;%d = %x&#8217;,0Ah<br />
db &#8216;%d = %x&#8217;,0</span></p>
<p><span style="font-family:&quot;font-size:medium;">Now, the values in the first 2 push Instructions is the Binary Representation of INT_MAX (2147483647 on my PC).<br />
Note that it is all ONE&#8217;s.</span></p>
<p><span style="font-family:&quot;font-size:medium;">When 1 is added to INT_MAX, look what happens to the next two push instructions.<br />
<span style="color:#cc33cc;">This number is 2147483648 but in unsigned notation&#8230;but in signed notation it&#8217;s -2147483648 since the Most Significant Bit decides the Sign of a Number. </span></span></p>
<p><span style="font-family:&quot;font-size:medium;">And since by default the compiler assumes that a variable is signed, you get the negative value.</span></p>
<p><span style="font-size:medium;"><span style="font-family:&quot;"><strong><span style="color:#cc33cc;">Try the same C code after replacing %d with the %u format specifier.</span></strong><br />
You&#8217;ll notice that 2147483648 will be the output. </span></span></p>
<p><span style="font-family:&quot;font-size:medium;">Remember, there&#8217;s nothing like postive or negative numbers&#8230;It&#8217;s all about interpretation. 111 (for a 3 bit architecture) could mean 7(without using MSB for sign-convention&#8230;ie. unsigned) as well as -1 (using MSB for sign ie. signed). </span></p>
<p><span style="font-family:&quot;color:#ffff00;font-size:medium;">You might wonder why I&#8217;ve blamed the Processor and not the compiler inspite of the fact that the Compiler has precalculated the result of addition and passed it to printf. </span></p>
<div><span style="font-family:&quot;color:#ffff00;font-size:medium;">The reason is that I&#8217;ve compiled the above code in Aggressive Optimization, so the compiler generates push instructions.<br />
Normally the Compiler plays safe and lets the processor take care of such values by generating the following code:<br />
</span></div>
<div><span style="font-family:&quot;color:#3333ff;font-size:medium;">mov eax, 1111111111111111111111111111111b<br />
lea     edx, [eax+1]<br />
push    edx</span></div>
<div><span style="font-family:&quot;font-size:medium;">I hope you now realise why this happens.</span></div>
<div></div>
<div>
<p align="center"><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="color:#ffffff;font-size:xx-small;">[EOF]</span></strong></div>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=8&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/14/0x6-datatype-range-violation-consequences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Microsoft Does it Differently</title>
		<link>http://born2c0de.wordpress.com/2006/10/13/0x5-microsoft-does-it-differently/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/13/0x5-microsoft-does-it-differently/#comments</comments>
		<pubDate>Fri, 13 Oct 2006 19:06:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Code Internals]]></category>
		<category><![CDATA[I/O : Interesting Observations]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/13/0x5-microsoft-does-it-differently/</guid>
		<description><![CDATA[I was just in the process of continuing my VB Disassembling Tutorial, when I was analyzing how a simple Library Function works. The function I was analyzing was the Len() Function. Contrary to our belief that this function, counts the number of characters in a string and returns it, this one does something totally different. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=7&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:130%;font-family:&quot;">I was just in the process of continuing my VB Disassembling Tutorial, when I was analyzing how a simple Library Function works.<br />
The function I was analyzing was the Len() Function. </span></p>
<p><span style="font-size:130%;font-family:&quot;">Contrary to our belief that this function, counts the number of characters in a string and returns it, this one does something totally different.</span></p>
<p><span style="font-size:130%;font-family:&quot;">Here&#8217;s how it works.</span></p>
<p><span style="font-size:130%;font-family:&quot;">When any string is stored in VB, it is automatically stored in this format (in unicode):<br />
</span></p>
<p><span style="font-size:130%;font-family:&quot;">Suppose the String is &#8220;ABC&#8221;:</span></p>
<p><span style="font-size:85%;font-family:&quot;color:#ffffff;">06 00 00 00 41 00 42 00 43 00</span></p>
<p><span style="font-size:130%;font-family:&quot;">The 41 to 43 part is a typical unicode style of storing strings, but 2 unicode characters before that, the number of bytes occupied by the string (including zeros) is stored.<br />
So 06 stands for 6 bytes occupied by &#8220;ABC&#8221; (since its stored as A,0,B,0,C,0)</span></p>
<p><span style="font-size:130%;font-family:&quot;">So all that the Len Function does is read 4 bytes before the beginning of the string and return that value itself.</span></p>
<div><span style="font-size:130%;font-family:&quot;">So actually, the Len Function does nothing except read the length from the string format and return it.</span></div>
<div></div>
<div><span style="font-size:130%;font-family:&quot;">Want to see how they do it?</span></div>
<div><span style="font-size:130%;font-family:&quot;">Here&#8217;s the Disassembled Listing of the Len() Function MSVBVM60.DLL DLL File.</span></div>
<div></div>
<div></div>
<div></div>
<div><span style="font-family:&quot;color:#3333ff;">__vbaLenBstr proc near </span></div>
<div></div>
<div></div>
<div><span style="font-family:&quot;color:#3333ff;">string = dword ptr 4 </span></div>
<div></div>
<div><span style="font-family:&quot;color:#3333ff;">mov eax, [esp+string] <span style="color:#cc33cc;">; eax points to string</span><br />
test eax, eax <span style="color:#cc33cc;">; ZF,SF,PF = EAX and EAX</span><br />
jz short break <span style="color:#cc33cc;">; If String is Null then break from loop</span><br />
mov eax, [eax-4] <span style="color:#cc33cc;">; Gets Unicode Length stored before string. </span><br />
</span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; Here&#8217;s how Text from textbox is stored</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; internally:<br />
; If text is born2c0de.<br />
; Then in memory:<br />
; (0&#215;12 0&#215;00) 0&#215;00 0&#215;00 (0&#215;62 0&#215;00 &#8230;)<br />
; ie, length of string in bytes(unicode)</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; (here 18bytes)<br />
; followed by a Unicode 0 (0&#215;00 0&#215;00) </span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; followed by the actual string in unicode</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; (&#8216;b&#8217; 00 &#8216;o&#8217; 00 &#8230; &#8216;e&#8217; 00)<br />
;<br />
; So [eax-4] just gets the unicode length of</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; string which already is stored when a</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; string is taken as<br />
; input from keyboard.<br />
;</span><br />
<span style="color:#cc33cc;">; How awesome, The Len() function doesn&#8217;t</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; even calculate length!!!</span><br />
shr eax, 1 <span style="color:#cc33cc;">; Divides Unicode Length by 2 to get Actual</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; Length.</span><br />
<span style="color:#cc33cc;">; Uses eax so it can be used as a return</span></span></div>
<div><span style="font-family:&quot;color:#3333ff;"><span style="color:#cc33cc;">; value.</span></p>
<p>break:</p>
<p></span></div>
<div><span style="font-family:&quot;color:#3333ff;">retn 4<br />
__vbaLenBstr endp<br />
</span></div>
<p><span style="font-size:130%;font-family:&quot;">This is real good. There are many more functions which are far more interesting and worth mentioning.</span></p>
<div><span style="font-size:130%;font-family:&quot;">I&#8217;ll be doing that as and when I get time.</span></div>
<p><span class="sg"></p>
<div>
<p align="center"><span style="color:#33ff33;"><strong><span style="color:#33ff33;">- Sanchit Karve</span></strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="font-size:78%;color:#ffffff;">[EOF]</span></strong></div>
</div>
<p></span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=7&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/13/0x5-microsoft-does-it-differently/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Cluttered is good&#8230;</title>
		<link>http://born2c0de.wordpress.com/2006/10/13/0x4-cluttered-is-good/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/13/0x4-cluttered-is-good/#comments</comments>
		<pubDate>Fri, 13 Oct 2006 18:53:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/13/0x4-cluttered-is-good/</guid>
		<description><![CDATA[There&#8217;s not much you can do when you have a typical day in the College Computer Lab. I tried to play around with some code after giving some thought to serious readability. [/sarcastic] Here&#8217;s what I did: #include &#60;stdio.h&#62; int main ( ) { printf ( &#8220;Ain&#8217;t this freakin&#8217; kewl?&#8221; ) ; return 0 ; } [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=6&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div><span style="font-family:&quot;font-size:medium;">There&#8217;s not much you can do when you have a typical day in the College Computer Lab.</span></div>
<div><span style="font-family:&quot;font-size:medium;">I tried to play around with some code after giving some thought to serious readability. [/sarcastic]</span></div>
<div></div>
<div><span style="font-family:&quot;font-size:medium;">Here&#8217;s what I did:</span></div>
<div></div>
<div><span style="font-family:&quot;color:#3333ff;font-size:x-small;">#include &lt;stdio.h&gt;</span></div>
<div><span style="font-family:&quot;color:#3333ff;font-size:x-small;">int main<br />
(<br />
)<br />
{<br />
printf</span></div>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> (</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> &#8220;Ain&#8217;t this freakin&#8217; kewl?&#8221;</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> </span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> )</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">;</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> return</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> 0</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;">;</span></p>
<p><span style="font-family:&quot;color:#3333ff;font-size:x-small;"> }</span></p>
<p><span style="font-family:&quot;font-size:medium;">The Prof came over, saw my code and commented:<br />
&#8220;If you don&#8217;t want to write the programs I ask you to do, that&#8217;s fine with me but at least don&#8217;t write something that won&#8217;t even work&#8230;&#8221; </span></p>
<p><span style="font-family:&quot;font-size:medium;">I smiled and said that the code would work. I compiled the code and showed it to the Prof.<br />
And guess what he says:&#8221;Must be something wrong with the Compiler&#8221;</span></p>
<p><span style="font-family:&quot;font-size:medium;">Our Prof. is such a dweeb&#8230;If only he knew what tokens are and how compilers process them for lexical analysis.<br />
If you provide more than one Space, it is considered as 1 (and the rest are ignored). </span></p>
<div><span style="font-family:&quot;font-size:medium;">Infact many websites challenge people to write C code that looks like trash but gets compiled.<br />
That&#8217;s called obfuscated C.</span></div>
<div></div>
<div><span style="font-family:&quot;font-size:medium;">Here are some neat links on code obfuscation.</span></div>
<div><a href="http://www.de.ioccc.org/years.html"><span style="font-family:&quot;font-size:medium;">International Obfuscated C Code Contest Winners</span></a></div>
<div><a href="http://www.cise.ufl.edu/~manuel/obfuscate/smile.c"><span style="font-family:&quot;font-size:medium;">Outputs a Smiley</span></a><br />
<a href="http://www.joe-ks.com/archives_jan2004/HumourousA.txt"><span style="font-family:&quot;font-size:medium;"> That&#8217;s beautiful ASCII Art</span></a></div>
<div><a href="http://www.cise.ufl.edu/~manuel/obfuscate/obfuscate.html"><span style="font-family:&quot;font-size:medium;">A good collection of selected Obfuscated Code..</span></a></div>
<div></div>
<div>
<p align="center"><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="color:#ffffff;font-size:xx-small;">[EOF]</span></strong></div>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=6&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/13/0x4-cluttered-is-good/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>Returning from main()</title>
		<link>http://born2c0de.wordpress.com/2006/10/13/0x3-returning-from-main/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/13/0x3-returning-from-main/#comments</comments>
		<pubDate>Fri, 13 Oct 2006 18:42:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[I/O : Interesting Observations]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/13/0x3-returning-from-main/</guid>
		<description><![CDATA[Everyone seems to be using void main() instead of int while writing C/C++ programs, inspite of void not being accepted as a return type for main in the C/C++ standard. I too used to do the same until some programmers on a Programming Forum told me a few years ago about the C++ Standard on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=5&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:130%;font-family:&quot;">Everyone seems to be using void main() instead of int while writing C/C++ programs, inspite of void not being accepted as a return type for main in the C/C++ standard.</span></p>
<p><span style="font-size:130%;font-family:&quot;">I too used to do the same until some programmers on a Programming Forum told me a few years ago about the C++ Standard on the return type of main().</span></p>
<p><span style="font-size:130%;font-family:&quot;">But what does void do anyway? Is it different from return 0?</span><br />
<span style="font-size:130%;font-family:&quot;">This doubt can be easily clarified by disassembling any C/C++ program and reading the code that is executed after main().</span><br />
<span style="font-size:130%;font-family:&quot;">On Windows, a start() function calls the main() function. But what about the return value? Is it read after main() quits?</span></p>
<p><span style="font-size:130%;font-family:&quot;">Let&#8217;s see&#8230;</span></p>
<p><span style="font-size:130%;font-family:&quot;">It doesn&#8217;t matter what data type you return from main(). Whether its int or void it doesn&#8217;t make any difference.<br />
Here&#8217;s what actually happens.<br />
In an executable file, the start() function which calls the main() function, expects a return value of data type integer so that it may decide what argument is passed to the exit() function which is called after main(). </span><br />
<span style="font-size:130%;font-family:&quot;">If void is chosen as the return type for main, the EAX register is passed as an argument to the exit function.</span><br />
<span style="font-size:130%;font-family:&quot;">Since the EAX register almost always contains the return value of any function, so if we say return 0, EAX would be set to zero, that&#8217;s all.</span><br />
<span style="font-size:130%;font-family:&quot;">Generally whenever void is used, EAX is set to zero at the end of main() , so it is exactly the same as passing return 0.</span><br />
<span style="font-size:130%;font-family:&quot;">But this is actually compiler dependant, so this cannot always be assumed.</span></p>
<p><span style="font-size:130%;font-family:&quot;">Ever wondered what happens to the return value after main() quits?</span><br />
<span style="font-size:130%;font-family:&quot;">Take a look at this disassembled listing of any C/C++ program.<br />
</span></p>
<div><span style="font-family:&quot;color:#3333ff;">; This is the main function</span></div>
<div><span style="font-family:&quot;color:#3333ff;">; This is how Borland Calls it.</span></div>
<div><span style="font-family:&quot;color:#3333ff;">; Other Compilers either have a direct call to Jump or like Visual C++, jump to an</span></div>
<div><span style="font-family:&quot;color:#3333ff;">; instruction which is another jump instruction to main(). Yes, lame code.</span></div>
<p><span style="font-family:&quot;color:#cc33cc;">call dword ptr [esi+18h]</span></p>
<p><span style="font-family:&quot;color:#cc33cc;">add esp, 0Ch </span><br />
<span style="font-family:&quot;color:#3333ff;">; (4 x 3) 12 bytes are cleared from the stack to clear space</span><br />
<span style="font-family:&quot;color:#3333ff;">; occupied by main()&#8217;s three arguments.</span><br />
<span style="font-family:&quot;color:#cc33cc;">push eax <span style="color:#3333ff;">; status for exit. Return value of main() is pushed<br />
</span>call _exit</span></p>
<p><span style="font-size:130%;font-family:&quot;">So no matter what you do, even if you ignore the return value type, the compiler will still pass the contents of the EAX Register into the exit function. This could lead to some unwanted results, and hence it is better to return 0 (ie. EXIT_SUCCESS) or 1(EXIT_FAILURE) depending on when and why you have designed the program to end. </span></p>
<p><span style="font-size:130%;font-family:&quot;">So even though, it is almost always the same, there is a reason why the standard recommends using int. Simply for the following reasons:</span></p>
<ol>
<li><span style="font-size:130%;font-family:&quot;color:#ff0000;">returning 0 by default is COMPILER DEPENDENT.</span></li>
<li><span style="font-size:130%;font-family:&quot;color:#ff0000;">No Guarantee about the content of the EAX Register after main() exits.</span></li>
<li><span style="font-size:130%;font-family:&quot;color:#ff0000;">Will not be portable to all Operating Systems.</span></li>
<li><span style="font-size:130%;font-family:&quot;color:#ff0000;">May cause incorrect termination of main()</span></li>
</ol>
<p><span style="font-size:130%;font-family:&quot;">So even though void main and return 0 are almost alike, we should try to avoid it&#8217;s use.</span></p>
<p><strong><span style="color:#ffffff;"></p>
<p align="center"><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478); </span></strong></p>
<div style="clear:both;padding-bottom:0.25em;"><strong><span style="font-size:85%;color:#ffffff;"><span style="font-size:78%;">[EOF</span>]</span></strong></div>
<p></span></strong></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=5&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/13/0x3-returning-from-main/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
		<item>
		<title>To typecast or not to typecast&#8230;</title>
		<link>http://born2c0de.wordpress.com/2006/10/12/0x2-to-typecast-or-not-to-typecast/</link>
		<comments>http://born2c0de.wordpress.com/2006/10/12/0x2-to-typecast-or-not-to-typecast/#comments</comments>
		<pubDate>Thu, 12 Oct 2006 17:01:00 +0000</pubDate>
		<dc:creator>[ b 2 c ]</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://born2c0de.wordpress.com/2006/10/12/0x2-to-typecast-or-not-to-typecast/</guid>
		<description><![CDATA[A lot of people still are confused about how characters can be compared to numbers. They believe that the characters are automatically converted into integers and then compared. Actually, there&#8217;s no need for a character to be converted in the first place as you shall see below. Have a look at this snippet. char x; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=4&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:130%;font-family:&quot;">A lot of people still are confused about how characters can be compared to numbers.</span><br />
<span style="font-size:130%;font-family:&quot;">They believe that the characters are automatically converted into integers and then compared.</span></p>
<p><span style="font-size:130%;font-family:&quot;">Actually, there&#8217;s no need for a character to be converted in the first place as you shall see below.</span></p>
<p><span style="font-size:130%;font-family:&quot;">Have a look at this snippet.</span></p>
<p><span style="font-size:130%;font-family:&quot;">char x;</span><br />
<span style="font-size:130%;font-family:&quot;">scanf(&#8220;%c&#8221;,&amp;x);</span><br />
<span style="font-size:130%;font-family:&quot;">if( x&gt;= 65 &amp;&amp; x &lt;= 90) printf(&#8220;Hello&#8221;);</span></p>
<p><span style="font-size:130%;font-family:&quot;">A question raised by a student was that how can a character be compared to a number? (which is a pretty acceptable query since that guy was new to the world of programming)</span></p>
<p><span style="font-size:130%;font-family:&quot;">But here&#8217;s what our professor replied:</span><br />
<em><span style="font-size:130%;font-family:&quot;color:#cc33cc;"><strong>C automatically typecasts the character into an integer and then performs comparision.</strong></span></em></p>
<div><strong><span style="font-size:130%;font-family:&quot;color:#ff0000;">WHAT ACTUALLY HAPPENS</span></strong></div>
<div><strong></strong></div>
<p><span style="font-size:130%;font-family:&quot;">Characters are stored in memory as Numbers. So Suppose an &#8216;A&#8217; is entered, it&#8217;s stored in a memory(a single byte) as (0&#215;41 or 65) in binary. Just because you specify the %c specifier in printf, it assumes the value to be an ASCII value of a character. No typecasting involved at all. Characters are treated as single-byte numbers.</span></p>
<p><span style="font-size:130%;font-family:&quot;">So Basically, there is nothing like a character in reality&#8230;everything is internally represented as a number&#8230;certain numbers correspond to a unique character according to ASCII Code and when the %s or %c format specifier is used, it requests the function that the argument that is being passed is to be displayed as the corresponding character of the actual number, instead of displaying the actual number itself.</span></p>
<p><span style="font-size:130%;font-family:&quot;">Luckily, people started to believe me after I showed them disassembled listings of programs.</span></p>
<p><span style="color:#33ff33;"><strong>- Sanchit Karve</strong></span><br />
<strong><span style="color:#3333ff;">[ b o r n 2 c 0 d e ]</span></strong><br />
<strong><span style="color:#ff0000;">printf(&#8220;I&#8217;m a %XR&#8221;,195936478);</span></strong><br />
<strong></strong><br />
<strong><span style="color:#ffffff;">[EOF]</span></strong></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/born2c0de.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/born2c0de.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/born2c0de.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/born2c0de.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/born2c0de.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=born2c0de.wordpress.com&amp;blog=4027573&amp;post=4&amp;subd=born2c0de&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://born2c0de.wordpress.com/2006/10/12/0x2-to-typecast-or-not-to-typecast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d8bbada932e15aac2faa5b14c41b26?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">[ b 2 c ]</media:title>
		</media:content>
	</item>
	</channel>
</rss>
