[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci201] / 08
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] <08> [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08]
Labs: [01] [02] [03] [04] [05] [06] [07] [09] [10]
Fri Feb 1 12:41:08 PST 2008

Contents


    pages: 70-73 Errors

      Class is canceled today -- 25th Anniversary [ http://25year.csci.csusb.edu/ ] -- but do the reading and leave your question under my office door before 12 noon today. I will have got the questions and answers onto this page by late Friday afternoon.

      I will leave the graded projects outside my door to be picked up. Don't forget you have the third iteration of your project [ projects.html#P3 ] due on Tuesday! I hope that the readings will provide some comfort as you add at least one loop so that the user can solve a series of problems with one run of the program.

      Reading -- Errors in Programs Pages 70-73

        The following notes can be downloaded [ 08.mp3 ] and played.

        Introduction

        Welcome to the 8th reading assignment from Skansholm on the kinds of errors that happen when we write programs.

        Compilation and Syntax

        The slightest mistyping will introduce a slew of compilation errors, so will any misunderstanding of the rules of the C++ language. You can reduce the number of compilation errors by learning the syntax rules and following them. You should also get into the habit of reviewing your code with a critical eye looking for bad punctuation and spelling. A computer is a lot less forgiving than a teacher of English!

        Execution and Bugs

        Execution errors are a kind of bug. They happen whenever we loose focus or when we've not checked that our coding is correct. Another source of execution errors is when the user does something we didn't plan for. There are advanced ways of handling this (exceptions) that we cover in CS202.

        Logic Errors and Bugs

        Most logic errors can be avoided if we always check our algorithm before we code it. Even so we also need to test the written code because it is easy to make mistakes in our logic.

        Debugging

        This is a good example of how a simple program can surprise us with unexpected and bad behavior.

      Questions

        When we write a loop must we separate the different conditions on different lines

        You don't have to. The compiler will compile the same condition independent of the layout you choose.

        But one way to make conditions clear and easier to understand is to separate them. When you format code tidily you will make fewer logic errors.

        Do we have a list of compiler errors and there meanings

        Not as far as I know. It is worth making your own personal list of the errors that happen in your own programs. Practice can help a lot in finding errors.

        There is one trick is to notice the line number of the error and look backwards from that line.

         file...............:Line#:....
         m_n_ms.cpp:22: error: `string' has not been declared

        The book lists three kinds of errors -- are there more

        There are a lot of programs that do precisely what is planned and expected of them. They fit the requirements that the programmers were given... However they don't do what is actually needed.

        We have a joke: that is not a bug, it is a feature.

        These requirements mistakes are the kind of error that I am very interested in. They are quite insidious. But they are a type of logic error.

        What is the most common error made by programmers

        Compiler -- depends on the how much experience the programmer has. But a common one is not declaring a variable or including a library.

        Execution -- Running of the end of an array.

        Logic -- off by one errors with indexes and counters.

        What is the most common Execution Error that you see

        Uninitialized variables. Especially when the variable is what we call a pointer -- and is supposed to store the address of some data.

        Can you identify execution errors before you run a program

        I can do this fairly well -- and I write my own code with enough care that I don't get very many -- except when I'm short of sleep.

        However a complex program can defeat the best of us with an unexpected error when it runs.

        How do you copy a file using ssh

        If you are logged into a Unix computer and want to copy a file called original to a file named new then you write the command
         		cp original new

        If you are running the Secure File Transfer Client then you can probably use the mouse to Right-Click the file and pick the 'Copy' item in the menu that pops up.

        (thank for a question that taught me a new technique).

        How exactly can one exploit code that has gone over the bounds of an

        array Perhaps you should go to the "Ethical Hacking" seminar the CSci club is running.

        The details depend very much on the machine and the mistake. I have an example in my cs202 labs. A badly designed login system that anybody can log into as long as they supply a long enough user name. What happens is that the user name overwrites the password field with something like 'xxxxxxxxxxx' and then you can use something like 'xxxx' to login.

        Is there a program that checks for errors automatically

        The complier tries to catch as many as possible. There was a program (lint) that looked for suspicious code that was probably an error... an example would be the following pattern
         		if ( ... = .... )

        But Computability theory proves that there is know computer program that will find all the errors in a computer program. We cover the details of the proof in the CSci546.

        Does C++ allow you to run the program line by line to find the errors like Visual Basic does

        Yes.

        What debugging aids are there in the Lab

        There is a Gnu Debugger (gdb) that does this.

        By the way -- it is a command line debugger and so can be used remotely via SSH. The KDE provides a GUI front end called "kdbg" that is under the "Start"->Deveopment->KDbg menu.

        You can find out more [ Gdb ] on the Wikipedia.

        On the other hand find most debuggers harder to use than thinking and planting extra outputs. I don't use it enough to be any good at it or to give you instructions and advice. I have a CS202 lab that Dr. Zemoudeh wrote that showed how to use gdb.... but I've discarded it in recent years. To be honest -- I feel debuggers are for wimps:-)

        A number of people have asked about these debuggers. I will see what documentation I can find... TBA.

        What is the difference between a compile error and a logic error

        Suppose that we programmed in English then
         Cat the mat on sat.
        is a compile (syntax) error. But
         Poor oil on burning water.
        is a logic error.

        In other words a logic error follows all the rules of the language but instructs the computer to something bad or at least unexpected.

        Will a program with logic errors compile

        Yes.

        How can this happen

        Because commands can make perfect grammatical sense and still be bad. Computers don't understand this and do it anyway. The key point of computer science is that computers are stupid but follow instruction precisely as written. Badly chosen instructions lead to erroneous behavior. The logic of the program is broken.

        It can be quiet subtle. For example: my wife is a substitute teacher for SBUSD but the new computer program can not be told that she is not in the house with out inputting her PIN... and a lot of other stuff. This compiled, runs, and makes us both want to smash the telephone. I class this as a logic error. Bit notice the program must have compiled an run without an execution error.

        Is there any reason for not using double

        Don't use double when you want to exact answers.

        As a rule you only use int's for counting. If you notice that you are doing lots of multiplications, and you don't mind an approximate answer then you can use double.

        There is another solution to the problem when you want to do precise (integer) calculations with very large integers. It is not a simple solution: you must use an vector of int's to represent numbers and work out operations for multiplying, adding and subtracting using multi-length arithmetic your self -- or hunt for the functions and classes on the Web.

        I'd love to spend time on this but it is not a very useful technique for everyday programmers. Perhaps I'll be able to put it in a future lab!

      Next

        Quiz 4 -- Arrays Vectors Characters Strings

        Lab 05

        TBA

        Project 3 Due on Loops

        [ projects.html#P3 ]

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End