Minimize Defects/Buggs in your code

Discussion in 'C' started by asadullah.ansari, May 13, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    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:
    #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:
    //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.
     
    Last edited: May 14, 2008
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    It's little bit info but important for developer. It should be as contributed not as article.
     
    shabbir likes this.
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its not always Article but the section is Article / Source code whatever is contributed.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice