Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Minimize Defects/Buggs in your code (http://www.go4expert.com/articles/minimize-defects-buggs-code-t10582/)

### Introduction

Here I am going to give a simple example of addition, substraction, multiplication etc to check overflow/underflow so that we can minimize buggs/defects in our code

### The code

In your project/product or any place, If you have some operation like Addition, substraction ,multiplication etc. then you have to compute very carefully.Because it can be Overflow or Underflow. You know due to this stupid careless, You may got bugs/defects in your project/product.

Idea is that when you are going to add two number, then you knows about their limitation.i.e. Every inbuilt data types has fixed length for a given machine size and compiler.So suppose in 32-bit machine for integer, it's size will be 32 bits so range will be -{2>>15} to {(2>>15)-1.

So we can say that
Maximum limit of signed integer will be MAX_SIZE_SINT = {(2>>15)-1}
Minimum linit of signed Integer will be MIN_SIZE_SINT = -{2>>15}

Code: C

`#include <stdio.h>#define MAX_SIZE_SINT  {(2>>15)-1}#define MIN_SIZE_SINT  {-(2>>15)}#define sint int  #define ZERO  0// Check for Addition sintAddChk(sint n1,   //First number       sint n2)   //Second Number{    if(n2 < ZERO) //for more Robustness    {        return SubChk(n1, -n2);    }    if(MAX_SIZE_SINT  - n2 < n1) //Overflow condition MAX_SIZE_SINT < n1+n2    {        printf("OverFlow because you can'nt go for number > %d",MAX_SIZE_SINT);        return MAX_SIZE_SINT;    }    return(n1 + n2);}// Ckecking under substractionintSubChk(sint n1,        sint n2){    if(n2 < 0)    {        return AddChk(n1, -n2);    }    if(INT_MIN + n2 > n1)     {        printf("UnderFlow because you can'nt go for number < %d",MIN_SIZE_SINT);        return MIN_SIZE_SINT;    }    return n1 - n2;}`

Code: C

`//checkiing under multiplicationsintMulChk(sint n1,       sint n2){    int IndSign = 1;    if(0 == n1 || 0 == n2)    {         return 0;    }    if(n1 < 0)     {        n1 = -n1;         IndSign = -IndSign ;     }    if(n2 < 0)     {         n2 = -n2;         IndSign = -IndSign ;     }    if((MAX_SIZE_SINT / n2) < n1)     {        if(Indsign>0)        {            printf("OverFlow because you can'nt go for number > %d",MAX_SIZE_SINT);            return MAX_SIZE_SINT ;        }        if(Indsign<0)        {            printf("UnderFlow because you can'nt go for number < %d",MIN_SIZE_SINT);            return MIN_SIZE_SINT ;        }    }    return IndSign * n1 * n2;}`

### Result

You can put more checks except this in your code like pointer instance, valid memory etc. to find bug easily i.e. at low cost.

Re: Minimize Defects/Buggs in your code

It's little bit info but important for developer. It should be as contributed not as article.

 shabbir 14May2008 07:44

Re: Minimize Defects/Buggs in your code

Its not always Article but the section is Article / Source code whatever is contributed.

 shabbir 2Jun2008 16:28

Re: Minimize Defects/Buggs in your code

Nomination for article of the month of May

 shabbir 18Jun2008 21:45

Re: Minimize Defects/Buggs in your code

Quote:
 Originally Posted by shabbir Nomination for article of the month of May
Start Voting now

 All times are GMT +5.5. The time now is 17:49.