29 Jan 1996

Outline

ObjectCenter hints

  1. Don't run ObjectCenter and your editor separately. This can confuse ObjectCenter, resulting in unfathomable error messages. Instead, type:
    setenv EDITOR vi   (or setenv EDITOR gnu-emacs)
    xobjectcenter
    
    Then, when you want to edit your file, pull down the "File" menu, and let go of the mouse on "Edit". This will ask you for the name of a file, and then bring up your editor. From then on, ObjectCenter will know about your editor, and automatically take you to the line that is causing a warning or error message.

    Note: If you forget to "setenv EDITOR gnu-emacs" before entering ObjectCenter, you should exit ObjectCenter and start over.

  2. If you are using an editor such as gnu-emacs or vi, be careful not to edit your file in the ObjectCenter window.
  3. Your program runs fine when compiled with gcc or some other compiler, but has errors or warnings at compile time or run-time when using ObjectCenter. What's wrong?

    One possibility is that ObjectCenter is detecting accesses to uninitialized data that the compiler doesn't check for. It may be that your program worked when compiled because the data happened to have had initial value zero. For example:

    street_seg *intersections[20];         // doesn't initialize array
    if (intersections[0] == NULL)          // but we might get lucky
    {
      intersections[0] = new street_seg;
    }
    

    If you're developing the program using something other than ObjectCenter and then doing your final work with ObjectCenter, it's a good idea to turn on all the warnings your initial compiler provides. With gcc, this is done with the -Wall option:

    gcc -Wall
    
    Note, however, than sometimes compilers give false warnings!

    Function declarations

    Consider this simple function example:

    int
    average(int a, int b) {   // this is called the function DEFINITON
      return (a + b) / 2;
    }
    
    For every function you should have a function declaration (also known as a function prototype) at the top of the file or in a header file. A function declaration is basically the part before the first brace with a semicolon following it. What this does is tells compiler what type function is and what type and how many arguments to expect, which it needs to know before the function is used. Don't need for main. Example of declaration:
    int average(int a, int b);  // don't actually need var names
    
    Actually this isn't needed at top of file, only sometime before it's called. But top of file is generally good so you can see at a glance what all your subroutines are. As programs get larger, will create a ``header file'', like ``blah.h'', that holds all this sort of header info.

    A few general rules for declaring functions:

    1. There should only be one definition of a function.
    2. Function definitions belong in .c files, not .h files.
    3. Function definition also serves as a function declaration.
    4. It's OK to have more than one declaration of a function, as long as they are the same.

    Variable declarations

    Similar rules apply also to variables. Look at the following example:

    int my_var;          // definition of global variable
    external int my_var; // declaration
    

    Rules for declaring variables:

    1. Variables should only be defined once.
    2. Definitions belong in .c files, not .h files.
    3. Definition also serves as declaration.
    4. Multiple (identical) declarations are OK.
    5. Variables must be declared before use.
    6. Declaration causes compiler/interpreter to allocate space for variable.

    Another example of recursion

    The following program computes Fibonacci numbers recursively (but not particularly efficiently). Go through a small example of how this program would execute, showing the call stack after each recursive call.

    int
    Fibonacci1(int n) {
      if(n <= 1) return 1;
      else return Fibonacci1(n - 1) + Fibonacci1(n - 2);
    }