Minimize Defects/Buggs in your code

asadullah.ansari's Avatar author of Minimize Defects/Buggs in your code
This is an article on Minimize Defects/Buggs in your code in C.
Rated 5.00 By 1 users

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
sint
AddChk(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 substraction
int
SubChk(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 multiplication
sint
MulChk(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.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
It's little bit info but important for developer. It should be as contributed not as article.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Its not always Article but the section is Article / Source code whatever is contributed.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for article of the month of May
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by shabbir
Start Voting now