Error

​Types of Errors

  • Syntax

  • Linker

  • Run-time

  • Logical

Syntax Errors

  • Violation of C++ syntax rules

  • Compile-time errors

  • Most common:

    • Missing a symbol, e.g. one of: , . ( ) { } [ ] ; + - * /

      • Most common: ; and }

    • Use before declaration

    • Putting a symbol where it doesn’t belong

    • 5 = x;

Linker Errors

  • Occur after compilation (during linking)

  • Executable file cannot be produced

  • Most common:

    • Missing header file

    • Incorrect header file

    • Incorrectly named function, e.g. Main instead of main

Runtime Errors

  • Occur during program execution

  • Not caught by compiler (usually no line number information)

  • Most common:

    • Divide by 0

    • Attempt to access inaccessible memory

      • Null pointer dereference

      • Segmentation Fault

    • Out of memory

Logical Errors

  • Occur during execution

  • Program runs, but gives incorrect result

  • Most common:

    • Off by one

    • Using = instead of == in a conditional

    • Infinite loops

    • Integer division

    • Array access out of bounds

    • Uninitialized variables

What to do when there's an error

  • If there is an error message, READ IT

    • Compile-time errors will tell you what the error was, which file contains the error and the number of the line at which the error was discovered.

    • Linker errors will tell you what thing couldn’t be found, from which you can often figure out why it couldn’t be found.

    • Runtime errors will tell you what went wrong, from which you can narrow down to where in the code the kind of thing that went wrong could have gone wrong.

    • If you don’t understand the error message, ask for help understanding the error

  • Start debugging

    • cout is your friend.

Writing code with fewer errors

  • Start small

  • Test often

  • Work on one thing at a time

  • Start debugging

    • cout is your friend.

Last updated

Was this helpful?