<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='http://wkfry.spaces.live.com/mmm2008-07-24_12.50/rsspretty.aspx?rssquery=en-US;http%3a%2f%2fwkfry.spaces.live.com%2fcategory%2f__x1NET%2ffeed.rss' version='1.0'?><rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:msn="http://schemas.microsoft.com/msn/spaces/2005/rss" xmlns:live="http://schemas.microsoft.com/live/spaces/2006/rss" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bill's Space: .NET</title><description /><link>http://wkfry.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&amp;_c=BlogPart&amp;partqs=cat__x1NET</link><language>en-US</language><pubDate>Tue, 20 May 2008 16:21:24 GMT</pubDate><lastBuildDate>Tue, 20 May 2008 16:21:24 GMT</lastBuildDate><generator>Microsoft Spaces v1.1</generator><docs>http://www.rssboard.org/rss-specification</docs><ttl>60</ttl><cf:parentRSS>http://wkfry.spaces.live.com/blog/feed.rss</cf:parentRSS><live:type>blogcategory</live:type><live:identity><live:id>-4671077372406318224</live:id><live:alias>wkfry</live:alias></live:identity><cf:listinfo><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="typelabel" label="Type" /><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="tag" label="Tag" /><cf:group element="category" label="Category" /><cf:sort element="pubDate" label="Date" data-type="date" default="true" /><cf:sort element="title" label="Title" data-type="string" /><cf:sort ns="http://purl.org/rss/1.0/modules/slash/" element="comments" label="Comments" data-type="number" /></cf:listinfo><item><title>Visual Studio 2005 and Arithmetic Overflow</title><link>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!233.entry</link><description>&lt;p&gt;Microsoft has made a terrible change between Visual Studio 2003 and Visual Studio 2005. (I personally find it completely irresponsible.) &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;&lt;span style="color:red"&gt;By default, Visual Studio 2005 projects have the “Check for arithmetic overflow/underflow” compilation switch &lt;u&gt;turned off&lt;/u&gt;.&lt;/span&gt; In Visual Studio 2003, this switch defaulted to true.&lt;/strong&gt;&lt;/blockquote&gt; &lt;p style="font-weight:bold;font-size:120%"&gt;What Does This Mean? &lt;p&gt;This means that if code attempts to multiply or add numbers that are very large (too large for the particular data type), the code will not throw an exception, but rather it will produce an unexpected result! This can lead to data corruption and potentially even infinite loops. (see “&lt;em&gt;How We Discovered The Problem&lt;/em&gt;” below…) &lt;p style="font-weight:bold;font-size:120%"&gt;Correcting the Problem &lt;p&gt;Every time you create a Visual Studio 2005 project: &lt;ul&gt; &lt;li&gt;Right-click on the project and bring up &lt;strong&gt;Properties&lt;/strong&gt;.  &lt;li&gt;Click on the &lt;strong&gt;Build&lt;/strong&gt; tab.  &lt;li&gt;Click on the &lt;strong&gt;Advanced…&lt;/strong&gt; button. (You may need to scroll down to find it.)  &lt;li&gt;Check the &lt;strong&gt;Check for arithmetic overflow/underflow&lt;/strong&gt; checkbox. &lt;/ul&gt; &lt;p style="font-weight:bold;font-size:120%"&gt;Why Did Microsoft Do That? &lt;p&gt;&lt;strong&gt;Performance.&lt;/strong&gt; &lt;p&gt;Checking for overflow requires a few extra programming cycles. Microsoft obviously traded safety for speed. (This is the equivalent of saying “I can make a car faster if I don’t include brakes”.) &lt;p style="font-weight:bold;font-size:120%"&gt;How We Discovered The Problem &lt;p&gt;I had written a little function that operated on a &lt;strong&gt;byte&lt;/strong&gt;. I wanted to write a Unit Test Case to ensure that the function worked as expected for all possible byte values, so I wrote the following code:&lt;code&gt;for(byte b=0; b &amp;lt;= 255; b++)&lt;br&gt;{&lt;br&gt;    myFunction(b);&lt;br&gt;}&lt;/code&gt;  &lt;p&gt;However, there is a serious bug in the code above. Once &lt;strong&gt;b == 255&lt;/strong&gt;, this function will try to increment &lt;strong&gt;b&lt;/strong&gt; one more time which should cause an overflow. Rather than overflowing, &lt;strong&gt;b&lt;/strong&gt; actually became &lt;strong&gt;0&lt;/strong&gt; and it became an infinite loop! &lt;p&gt;I was shocked that I did not get an overflow exception, so then I tried…&lt;code&gt;byte b = byte.MaxValue;&lt;br&gt;b++;&lt;br&gt;Console.WriteLine(b);&lt;/code&gt;  &lt;p&gt;And I received &lt;strong&gt;0&lt;/strong&gt;! &lt;p&gt;So I tried…&lt;code&gt;int i = int.MaxValue;&lt;br&gt;i++;&lt;br&gt;Console.WriteLine(i);&lt;/code&gt;  &lt;p&gt;And I received &lt;strong&gt;a large negative number&lt;/strong&gt;. &lt;p&gt;And finally, I tried…&lt;code&gt;int i = int.MaxValue;&lt;br&gt;i *= 10;&lt;br&gt;Console.WriteLine(i);&lt;/code&gt;  &lt;p&gt;And I received &lt;strong&gt;-10&lt;/strong&gt;!!! &lt;p&gt;&lt;em&gt;At this point, I knew something was terribly wrong…&lt;/em&gt; &lt;p style="font-weight:bold;font-size:120%"&gt;What If I Know That My Code Can’t Overflow? &lt;p&gt;In general, it’s better for us to be safe than sorry and always checked for overflow. HOWEVER, if you are 100% certain that you have a function that could never overflow, C# provides the &lt;span style="font-family:'Courier New'"&gt;unchecked { }&lt;/span&gt; block that allows you to mark a block of code to not require overflow checking. &lt;p&gt;I strongly advise against any developer actually using the unchecked statement unless you really, really, really know what you are doing!&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-4671077372406318224&amp;page=RSS%3a+Visual+Studio+2005+and+Arithmetic+Overflow&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wkfry.spaces.live.com&amp;amp;GT1=wkfry"&gt;</description><comments>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!233.entry#comment</comments><guid isPermaLink="true">http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!233.entry</guid><pubDate>Sat, 28 Apr 2007 20:08:38 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wkfry.spaces.live.com/blog/cns!BF2CFFE0D35B3B70!233/comments/feed.rss</wfw:commentRss><wfw:comment>http://wkfry.spaces.live.com/Blog/cns!BF2CFFE0D35B3B70!233.entry#comment</wfw:comment><dcterms:modified>2007-07-29T20:25:39Z</dcterms:modified></item></channel></rss>