Advertisement
ItzEdInYourBed

69 - C++ Errors

Jul 12th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. [SIZE=7][B]Introduction to Bugs[/B][/SIZE]
  2. [I]“First actual case of bug being found.”[/I]
  3. The story goes that on September 9th, 1947, computer scientist [URL='https://en.wikipedia.org/wiki/Grace_Hopper']Grace Hopper[/URL] found a moth in the Harvard Mark II computer’s log book and reported the world’s first literal computer bug. However, the term “bug”, in the sense of technical error, dates back at least to 1878 and with Thomas Edison.
  4. On your programming journey, you are destined to encounter innumerable red errors. Some even say, that debugging is over 75% of the development time. But what makes a programmer successful isn’t avoiding errors, it’s knowing how to find the solution.
  5. In C++, there are many different ways of classifying errors, but they can be boil down to four categories:
  6. [LIST]
  7. [*][B]Compile-time errors:[/B] Errors found by the compiler.
  8. [*][B]Link-time errors:[/B] Errors found by the linker when it is trying to combine object files into an executable program.
  9. [*][B]Run-time errors:[/B] Errors found by checks in a running program.
  10. [*][B]Logic errors:[/B] Errors found by the programmer looking for the causes of erroneous results.
  11. [/LIST]
  12. In this mini-lesson, we will be looking at different errors and different error messages, and we’ll teach you how to think about errors in your code a little differently.
  13.  
  14. [SIZE=7][B]Compile-time Errors[/B][/SIZE]
  15. When we are writing C++ programs, the compiler is our first line of defense against errors.
  16. There are two types of compile-time errors:
  17. [LIST]
  18. [*][B]Syntax errors[/B]: Errors that occur when we violate the rules of C++ syntax.
  19. [*][B]Type errors[/B]: Errors that occur when there are mismatch between the types we declared.
  20. [/LIST]
  21. Some common syntax errors are:
  22. [LIST]
  23. [*]Missing semicolon[ICODE] ;[/ICODE]
  24. [*]Missing closing parenthesis [ICODE])[/ICODE], square bracket [ICODE]][/ICODE], or curly brace [ICODE]}[/ICODE]
  25. [/LIST]
  26. Some common type errors are:
  27.  
  28. [LIST]
  29. [*]Forgetting to declare a variable
  30. [*]Storing a value into the wrong type
  31. [/LIST]
  32. Here’s an example of a compile-time error message:
  33.  
  34. [IMG]https://i.imgur.com/IvVqup7.png[/IMG]
  35. The compiler will tell us where (line number) it got into trouble and its best guess as to what is wrong.
  36.  
  37. [SIZE=7][B]Link-time Errors[/B][/SIZE]
  38. Sometimes the code compiles fine, but there is still a message because the program needs some function or library that it can’t find. This is known as a link-time error.
  39. As our program gets bigger, it is good practice to divide the program into separate files. After compiling them, the [I]linker[/I] takes those separate object files and combines them into a single executable file. Link-time errors are found by the linker when it is trying to combine object files into an executable file.
  40. Some common link-time errors:
  41. [LIST]
  42. [*]Using a function that was never defined (more on this later)
  43. [*]Writing [ICODE]Main()[/ICODE] instead of [ICODE]main()[/ICODE]
  44. [/LIST]
  45. Here’s an example of a link-time error message:
  46. [IMG]https://i.imgur.com/8cyZphr.png[/IMG]
  47. These errors are more hard to find, but remember, [URL='https://www.google.com/']Google[/URL] is your friend! Here, a good Google search would be: “C++ undefined reference to main”.
  48.  
  49. [SIZE=7][B]Run-time Errors[/B][/SIZE]
  50. If our program has no compile-time errors and no link-time errors, it’ll run. This is where the fun really starts.
  51. Errors which happen during program execution (run-time) after successful compilation are called run-time errors. Run-time errors occur when a program with no compile-time errors and link-time errors asks the computer to do something that the computer is unable to reliably do.
  52. It happens after we give the [ICODE]./[/ICODE] execute command:
  53. [CODE]./a.out[/CODE]
  54. Some common run-time errors:
  55.  
  56. [LIST]
  57. [*]Division by zero also known as [I]division error[/I]. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs.
  58. [*]Trying to open a file that doesn’t exist
  59. [/LIST]
  60. There is no way for the compiler to know about these kinds of errors when the program is compiled.
  61.  
  62. Here’s an example of a run-time error message:
  63.  
  64. [IMG]https://i.imgur.com/eFHTchi.png[/IMG]
  65.  
  66. [SIZE=7][B]Logic Errors[/B][/SIZE]
  67. Once we have removed the compile-time errors, link-time errors, and run-time errors, the program runs successfully. But sometimes, the program doesn’t do what we want it to do or no output is produced. Hmmm…
  68. These types of errors which provide incorrect output, but appears to be error-free, are called logical errors. These are one of the most common errors that happen to beginners and also usually the most difficult to find and eliminate.
  69. Logical errors solely depend on the logical thinking of the programmer. Your job now is to figure out why the program didn’t do what you wanted it to do.
  70. Some common logic errors:
  71. [LIST]
  72. [*]Program logic is flawed
  73. [*]Some “silly” mistake in an [ICODE]if[/ICODE] statement or a [ICODE]for[/ICODE]/[ICODE]while[/ICODE] loop
  74. [/LIST]
  75. [B]Note:[/B] Logic errors don’t have error messages. Sometimes, programmers use a process called [URL='https://en.wikipedia.org/wiki/Test-driven_development']test-driven development (TDD)[/URL], a way to give logic errors error messages and save yourself a lot of headaches!
  76.  
  77. [SIZE=7][B]Review[/B][/SIZE]
  78. Finding bugs is a huge part of a programmer’s life. Don’t be intimidated by them… embrace them. Errors in your code mean you’re trying to do something cool!
  79. In this lesson, we have learned about the four types of C++ errors:
  80. [LIST]
  81. [*][B]Compile-time errors:[/B] Errors found by the compiler.
  82. [*][B]Link-time errors:[/B] Errors found by the linker when it is trying to combine object files into an executable program.
  83. [*][B]Run-time errors:[/B] Errors found by checks in a running program.
  84. [*][B]Logic errors:[/B] Errors found by the programmer looking for the causes of erroneous results.
  85. [/LIST]
  86. Remember, [URL='https://www.google.com/']Google[/URL] and [URL='https://www.stackoverflow.com/']Stack Overflow[/URL] are a programmer’s best friends. For some more motivation, check out this blog post: [URL='https://news.codecademy.com/errors-in-code-think-differently']Thinking About Errors in Your Code Differently[/URL].
  87. We wish you the best of luck in your bug-squashing journey.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement