Wednesday 7 January 2015

C Programming Error Types – Runtime, Compile & Logical Errors


C Programming Error Types – Runtime, Compile & Logical Errors

------------------------------------------------------------------------------------------------------------------

Basically there are three types of errors in c programming:
  1. Runtime Errors
  2. Compile Errors
  3. Logical Errors
------------------------------------------------------------------------------------------------------
  1. Runtime Errors
An error that occurs during the execution of a program. In contrast, compile-time errors occur while a program is being compiled. Runtime errors indicate bugs in the program or problems that the designers had anticipated but could do nothing about. For example, running out of memory will often cause a runtime error.
The runtime errors detected by Reactis for C include:
  • Overflow Numeric calculations which produce a result too large to represent.
  • Divide by Zero Dividing a numeric value by zero.
  • Invalid Shift Shifting an integer value by an amount which produces an undefined result according to the C standard.
  • Memory Errors Accessing an invalid memory region in a way which produces an undefined result, such as accessing an array outside its bounds or accessing heap-allocated memory after the memory has been freed.
  • Uninitialized Data Access Accessing memory before the memory has been initialized, so that the result of the access is undefined under C semantics.
  1.  Fatal Errorsfatal error is basically when the executable crashes.
    Example 1: The program divided by zero, as in:
    int scores = 500;
    int num = 0;
    int avg;
    avg = scores / num;
    
    The program would crash saying:
    Floating exception
    
    Example 2: Segmentation faults, Bus errors.
    These occur when you try to access memory that your program is not allowed to use or that doesn't exist in the computer (i.e., there is only so much memory in the computer).


    Aside: Even virtual memory has limits.
    Your program will crash giving the "Segmentation fault" or "Bus error" message.
    These errors often occur due to improper use of arrays or pointers.
    Example: Using an uninitialized array index...
    int values[10];
    int i;
    cout << "The ith value is: " << values[i] << endl;
    
    may cause such an error. These, particularly, are tricky since they may or may not occur based on what the initial garbage value of the index is when you run the program. Remember, you cannot generally assume variables get initialized to zero.                          -----------------------------------------------------------------------------------------------------
  2.  Compile Errors
Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code for possible errors.

compile time error example :
  1.  Compiler Warningscompiler warning indicates you've done something bad, but not something that will prevent the code from being compiled.
    You should fix whatever causes warnings since they often lead to other problems that will not be so easy to find.
    Example: Your code calls the pow() (raise to a power) library function, but you forgot to include math.h.
    Because you've supplied no prototype for the pow() function (its in math.h), the compiler warns you that it assumes pow() returns an int and that it assumes nothing about pow()'s parameters:
    somefile.cpp:6: warning:
      implicit declaration of function `int pow(...)'
    
    This is a problem since pow() actually returns a double. In addition, the compiler can't type-check (and possibly convert) values passed to pow() if it doesn't know how many and what type those parameters are supposed to be.


    Note: The compiler will label warnings with the word warning so that you can distinguish them from errors.
  2.  Compiler Errorscompiler error indicates something that must be fixed before the code can be compiled.
    Example: You forget a semi-colon (;) at the end of a statement and the compiler reports:
    somefile.cpp:24: parse error before `something'
    


    Always remember to fix the first few errors or warnings, since they may be causing all the rest.Compiler messages usually list the file and line number where a problem occurs. Nonetheless, errors often occur on the lines prior to what the error message lists. Especially check the line immediately preceding where the error message indicates.
    Finally, note that some compilers may choose to call something an error while others may just call it a warning or not complain at all.
----------------------------------------------------------------------------------------------------------------
3. Logical Errors
In computer programming, a logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash). A logic error produces unintended or undesired output or other behavior, although it may not immediately be recognized as such.

For example, assigning a value to the wrong variable may cause a series of unexpected program errors. Multiplying two numbers instead of adding them together may also produce unwanted results.

Also, logical errors could not be detected by the compiler, and thus, programmers has to check the entire coding of a c program line by line.

A program with logical errors is compiled (translated) and run successfully but it does not give correct result.
  • The sequence of instructions used in a program may be incorrect.
  • The mathematical formulas used in program instructions may be incorrect etc.
The logical errors are difficult to detect. Logical errors can only be detected by examining all the units of the program one by one. It is a very time consuming and lengthy process.

Logic Errorslogic error occurs when your program simply doesn't do what you want it to.
Example: You have an infinite loop because you did not update the variable(s) used in the condition of a loop, as in:
cin >> account_num;

Assume user did not enter -1.

while (account_num != -1) {
  cout << "Account #: " << account_num << endl;
  ProcessAccount(account_num);
  // Oops...Forgot to read another account # here!
}

No comments: