ERRORS IN C++

ERRORS IN C++

No one is perfect, everyone commits some errors in one form or the other.  Programming errors often remain undetected until an attempt for compiling or executing the program is made.  Since errors prevent the program from being compiled or executed successfully, one should remove them before compiling and executing.  
      Error is an illegal operation performed by the user which results in abnormal working of the program.  The common type of errors that can occur are: 
1) Syntax Errors (compile - time errors) 
2) Run time Errors 
3) Linker Errors 
4) Logical Errors (Semantic Errors) 

SYNTAX ERRORS 

Syntax errors are those errors that occur when you violate the  rules of writing C ++ syntax.  All these errors are detected by the compiler so it is also known as compiler errors.  Some of the common compile time errors are missing semicolons at the end of the statements, reference to an undeclared variables, wrong number or type of arguments passed to a function etc. 


NOTE: Here the dotted circles represent the location of errors The errors are as follows: 
1.  The spelling of cout is written as cou.  
2. The second cout statement does not end with a semicolon.  
3.  The variable div is not declared as integer variable.  
4.  The program doesn't end with a closing brace (}).  

RUN TIME ERRORS 

Run time errors occurs during program execution after successful compilation.  Some common run time errors are division by zero (Divide Error), an attempt to compute the logarithm or the square root of a negative number (Domain Error), null pointer assignment, stack overflow etc. 
       Run Time Errors are difficult to find as the compiler does not point to the line at which this error occurs.  The error message is displayed only after the execution with a wrong output.  
Now, consider the previous program in which all the syntax errors are removed and the program is successfully compiled.  On execution of the program, if value of b is entered as then it will generate a DIVIDE ERROR as division by zero is not possible.  This will stop the program's execution without displaying the answer.  

LINKER ERRORS 

Linker errors normally occur when after compilation we link the different object files with main's object file using Ctrl + F9 key (Run).  Some common linker errors are writing maid () instead of main (), any user - defined function is called but not defined, any standard built - in function (sqrt, cos etc.) is called but its definition which is stored in library file  which is not present.
  The Linker must find the definition of the function called otherwise it will not create an executable file.  To correct these errors return back to program using F6 key.  

LOGICAL ERRORS (SEMANTIC ERRORS)

Errors like syntactic and execution errors are easier to find because they can just be located by generation of error messages on compilation or execution of a program but some programs do not provide desired output when certain input values ​​are supplied to it.  Such errors which provide incorrect output but appears to be error free are called logical errors.  Logical errors are often difficult to detect.  The technique of finding and correcting this type of errors is called logical debugging.  
The Hit and Trial Method is one of the best method of finding logical errors.
  First the detection of errors and then the correction of errors are to be accomplished detection can be accomplished by testing a new program with data that will yield a known answer.  If correct results are not obtained, the program contains logical errors and needs to be debugged.  
         After detection comes the correction.  In this careful study of program is carried out and results are checked for various inputs to locate errors.  But if errors exist beyond the scope of the programmer then there may be some hardware errors or compilation errors.  These conditions are very rare.  The following program illustrate a logical error.  
   Program to evaluate the Expression
y=(x/x+1)+1/2(x/x+1)^2+1/3(x/x+1)^3+1/4(x/x+1)^4
 
On execution the output comes out to be
      x = 2      y = 3.358796 
but, as done by copy and pencil, output turns out to be 
       x = 2    y = 1.04 (approx) 
  on close inspection, we found the assignment statement should be corrected to  
       z = x / (x + 1);  
But still output comes out to be: 
        x = 2     y = 0.789523 
Again, on close inspection it was found that the second assignment statement should be corrected to 
        y = z + pow (z,2) / 2 + pow (z, 3)  / 3 + pow (z, 4) / 4;  
and finally on modification the result turns out as per requirements: 
       x = 2      y = 1.037037

FLOWCHART SHOWING ERRORS 

We write the program in the Editor Window with the help of various keys.  Then, we compile the program and check for syntax errors.  If this exists any syntax errors then we move back to program in editor window and correct the program.  Then after correcting all the syntax errors, the program is linked with the help of linker.  The linker links the library files and other object files with the main's object file.  Then it checks whether linker errors exist on not.  If it exists then it should be removed by checking the path of library files and function definitions.  After removing the linker errors, the program is executed for all possible input values.  If no logical or run time errors are present then correct output is displayed on the screen otherwise correct it.


No comments

Powered by Blogger.