1. Goals for this week

  1. Learn how to use linux man pages

  2. Preview global variable use (for next lab)

  3. Some reminders about using gdb and valgrind for debugging C programs

2. Starting Point Code

Start by creating a 8 directory in your cs31/weeklylabs subdirectory and copying over some files:

$ cd ~/cs31/weeklylabs
$ mkdir week08
$ cd week08
$ pwd
  /home/you/cs31/weeklylabs/week08
$ cp ~adanner/public/cs31/week08/* ./
$ ls
globalvars.c Makefile

3. man and manpages

First, we are going to learn how to use man to read manual pages, and how to use apropos to find commands: man and apropos

Next, let’s look at the man page for gettimeofday to see what it tells us about this functions.

man gettimeofday
man 2 gettimeofday   # or explictly specify the manual section:
              # (system call gettimeofday is in section 2 of the manual)

apropos is a command for finding the names of other commands or library functions. It is useful if you cannot remember the name of a library function or command but you know what it does. Suppose that we cannot remember gettimeofday, we could try to find it using apropos:

apropos day

apropos time also works, but is more verbose as there are many more time related commands/functions.

4. C Programming: Global Variables

For next week’s lab, we will have a specific application where we want to use a small number of global variables. Global variables are variables declared outside a function definition. They are always in scope, and the space for them persists for the entire run of the program. We will briefly show an example use of global variables this week in the sample code. Though we will not need this feature for the Game of Life lab, it will serve as a preview for next week.

Let’s open globals.c and take a look at it.

The static modifier to their declaration limits the scope to functions within this .c file (only those can name these global variables), but there storage space still persists for the entire run of the program.

You should generally avoid using global variables as much as possible, but sometimes you must use them when you need a value to persist between function calls (beyond the return from a function that could create it as a local…​ i.e. there is no function that could be on the call stack and have this as a local).

Using Global Variables

This example is not an obvious need for a global variable, but if this code was being used to implement a library, then there may be a real need for using a global variable. . For now, I just want you to see and use global variables. . However, you should avoid using global variables in your program except when explicitly allowed.

In general, you should always design functions to take as parameters the values they need; if a function needs a value, pass it in, and if a caller needs a value from the function return it (or pass in pass-by-reference style parameters through which the function can modify the caller’s argument values). This makes your code more generic than using globals, and it avoids allocation of space for the entire run of your program when it is not needed.

5. Reminder about gdb and valgrind for C program debugging

We are back to C programming this week, with pointers and functions.

In lab on Weekly Lab 5 we looked at some examples of using both gdb and valgrind. Take a look at the Weekly Lab 5 weekly lab page and the examples we talked about as a reminder of how to use these debugging tools.

6. Handy References