Solved

Learning C# - OverFlowChecker?

Posted on 2007-03-25
2
188 Views
Last Modified: 2013-12-17
Im reading Beginning Visual C# and in a section on Type Conversions we are introduced to "Overflow Checking". Pretty cool, but the author goes on to illustrate how Overflow Checking can be configured at the project level. This book - I later found out - is a bit old and references an older version than i'm using (Visual Studio 2K5).  

Now for the questions...
1. Overflow Checking seems to be a good idea, so should it ALWAYS be used ...by default?
2. If not, why not?
3. If so, how (in Visual Studio 2k5) can this be set up (again my book navigates through options I dont seem to have)

thanks,
sleeper
0
Comment
Question by:the_sleeper
2 Comments
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18788937
1)  NO
2)  There's a performance penalty, so it should only be turned on during Debug/testing
     There are some algorithms that need to ignore overflows, not trap them.  An example would be a checksum, as well as some random and encryption algorithm.  In other words, these rely on arithemetic being done in modulus 2 ^ 32, where the high bits are just ignored.

You should also be aware that it's only done on integer arithmetic; it doesn't work for floats, decimals, etc.  Also, a lot of people don't know that division by zero does NOT produce an overflow error; this always throws a DivideByZeroException.

Also, you can always use the "checked" and "unchecked" keywords where you definitely want to trap or ignore this.   You'll have much more control this way.

For VS 2005, you select the global option this way:

With a project selected in Solution Explorer, on the Project menu.
click Properties.

Click the Compile tab.

Click the Advanced Compile Options button.

In the Advanced Compiler Settings dialog box, there is a Remove integer
overflow checks check box.
0
 

Author Comment

by:the_sleeper
ID: 18789032
Dude...Excellent Answer!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question