Hardware, Software & Product Development | Sparx EngineeringHardware, Software & Product Development | Sparx EngineeringHardware, Software & Product Development | Sparx EngineeringHardware, Software & Product Development | Sparx Engineering
  • Home
  • Expertise
    • Software Engineering
    • Electrical Engineering
    • Chemical Products and Services
    • Biomedical Engineering
    • Mechanical Engineering
    • Production Management
    • Automation
    • Industrial Design
  • Blog
  • About Us
NextPrevious

Detecting arithmetic overflow in C#

By rajaz | Software | 0 comment | 19 June, 2013 | 0

Recently I was interfacing with an accelerometer sensor, and the sensor was streaming raw acceleration values. The C# program processing the data seemed to work as I was getting reasonable output (the output was aggregate of few hundred samples). However while debugging an unrelated problem, I happened to realize that for some streaming values I was having an overflow condition. Although I should have been vigilant, for the present case I found the overflow condition purely by chance. I wasn’t running the debugger because I was getting unexpected results, and what the problem was about I normally wouldn’t be looking at data processing code that had overflows. That made me explore if the C# compiler in Visual Studio 2012 provides any features to detect unsuspecting overflows. I learned that there are two ways for detecting arithmetic overflow in C#.

Configure project build settings

In project properties, go to Build -> Advanced Build Settings and

advanced_build_settings

Use checked/unchecked

The checked/unchecked keyword allows explicit control over the checking of overflow condition for arithmetic operations. If the overflow check is enabled using checked and an overflow condition arises, an OverflowException is thrown. Similarly, the unchecked keyword can be used to disable overflow check for a certain piece of code, if the overflow check is enabled otherwise. The checked/unchecked keywords can be used in expression form or a block form.

The example usage of checked in expression form is:

[code language=”csharp”]
try
{
Int32 int1 = Int32.MaxValue;
Int32 int2 = checked(int1 + 1);
}
catch (OverflowException ex)
{
Debug.WriteLine(ex.Message);
}
[/code]

The example usage of checked in block form is

[code language=”csharp”]
try
{
checked
{
Int32 int1 = Int32.MaxValue;
Int32 int2 = int1 + 1;
}
}
catch (OverflowException ex)
{
Debug.WriteLine(ex.Message);
}
[/code]

The MSDN documentation for unchecked says the following if performance is a concern:

Because checking for overflow takes time, the use of unchecked code in situations where there is no danger of overflow might improve performance. However, if overflow is a possibility, a checked environment should be used.

Nevertheless I think it is a good idea to do development and testing with overflow check turned on to identify potential overflow issues.

C#, overflow

rajaz

More posts by rajaz

Related Posts

  • Reading line-by-line from a serial port (or other byte-oriented stream)

    By Ben Voigt | 10 comments

    With many .NET developers moving from the traditional (and broken) System.IO.Ports.SerialPort DataReceived event handling to either the correct and more efficient BaseStream.BeginRead / BaseStream.EndRead pair I promoted in my last post or the newer BaseStream.ReadAsyncRead more

  • How to Model NPT Threads in Solidworks

    By rmontifar | 3 comments

    National Pipe Thread Taper or NPT threaded pipes and fittings are deployed in a variety of fields where transportation or containment of liquids, gases, steam, or hydraulic fluid is required. The NPT geometry allows internalRead more

  • Multi-Tiered Linux Backup System – Part I

    By dreynolds | 0 comment

    Backing up important data and memories is an important task that should not be neglected. Just as important as performing Linux backups is verifying that the backups made are good and can be used toRead more

  • Clojure: An improved workflow

    By dfohl | 0 comment

    Like many beginning Clojure programmers, I started off following Stuart Sierra’s “Reloaded” workflow guide. While it was a great starting point, there were a number of things that I wanted to change. If the projectRead more

  • Start Zoneminder Recordings with Vera Events

    By dsmoot | 4 comments

    In a previous post I explained how you could configure the security DVR software Zoneminder to trigger recordings from a network connection. While a neat trick, I never really explained why I set this up.Read more

Leave a Comment

Cancel reply

Your email address will not be published. Required fields are marked *

NextPrevious
  • Home
  • Expertise
  • Blog
  • About Us
Sparx Technologies, LLC. dba Sparx Engineering © 2009 - 2021 | All Rights Reserved
  • Home
  • Expertise
    • Software Engineering
    • Electrical Engineering
    • Chemical Products and Services
    • Biomedical Engineering
    • Mechanical Engineering
    • Production Management
    • Automation
    • Industrial Design
  • Blog
  • About Us
Hardware, Software & Product Development | Sparx Engineering