Frequently Asked Questions about Debugging


Compiling/Linking

Q.
When I try to compile my program, the error messages go off the screen (too many). What can I do so that I can see the top of the list?

A.
You can scroll the window by dragging the scrollbar with the middle mouse button (on UNIX machines) or pipe the output to "less", by typing the following at the UNIX prompt:

compile-command |& less

(Make sure to replace compile-command with the UNIX command you use to compile.)

The "|" pipes standard output into the program less and the "&" makes sure standard error goes there too.


Q.
When compiling, I get a "compilation of header file requested" message?

A.
Sounds like you are trying to compile a .h file? Only .c files need to be compiled, since .h files are #include'd.


Q.
The compiler gives me an error like:
"somefile.c", line 85: warning(1116):
non-void function "SomeFunction"
          (declared at line 51) should return a value
  } 
  ^

A.
Here's likely the problem...

  if (some-condition)
    return some-value;
}

It is possible (though not if only valid data is used) for someone to get past all the ifs in the function. Note that after this last if (listed above), there is no code to return anything. Now, since the function returns "int" and the compiler has identified a case in which it is possible that nothing gets returned, it complains.

Fix it so that the function always returns something, and likely the error will go away.


Q.
When I compile my program with the command:
gcc prog.c

I get:

/var/tmp/cc2sdOi_1.o: In function `main':
/var/tmp/cc2sdOi_1.o(.text+0x4): undefined reference to `Foo'
What's the problem?

A.
First, make sure you spelled the function "Foo" the same when you prototype, define and call it (remember, upper/lowercase must match too). If that's ok...

Make sure you included all the .c files that make up the program in the compilation command, as in:

gcc prog.c module1.c module2.c ...

If this function (Foo()) is defined in a module (i.e., not in prog.c), that module must be compiled with the rest of the program. This is necessary even if you correctly include the header files for the module (i.e., if you have a prototype for that functon). Prototypes don't produce any code, they only tell the compiler what type of things a function takes and returns. The function definitions define the code for a function.

In any case, it is probably better to write a Makefile for programs with many source code files.


Q. [New]
When I compile my program, it complains about "redefinition of SomeFunc"...but I'm sure I define it in only one .c file?

A.
Are you #include'ing the ".c" file? Only include ".h" files.


Q.
The compiler says: "...warning: comparison between pointer and integer"?

A.
Often, this has to do with a missing prototype. When the compiler has no prototype for a function, it assumes that the function returns an int. Missing a prototype, of course, is bad since the compiler cannot check the parameters (to make sure they are the right type or to convert them to the right type) and cannot check the return type. Adding the prototype or including the correct header (which has the prototype) is the solution.


Q.
What does "redefinition of 'some-data-type'" mean (as an error message)?

A.
It probably means some header file is included twice (either directly or indirectly). It's ok for headers to be included multiple times (more often indirectly), but you must wrap these headers?


Q.
What does "request for member 'fieldName' in something not a structure or union" mean (as an error message)?

A.
It probably means you are trying to do something like:

var.fieldName

where var is not a structure, and thus, has no field fieldName.


Q.
What does the compiler error "dereferencing pointer to incomplete type" mean?

A.
It probably means you are trying to dereference a pointer to a struct (i.e., use star (*) or arrow (->) with it) in code where that struct is not defined. Are you trying to dereference an ADT outside of the .c file where its CDT is defined?


Q.
When compiling, I get: "someheader.h: unterminated '#if' conditional"?

A.
You are missing the #endif that goes at the end of the header.


The Debugger (GDB)/Manual Debugging

Q.
When I run my program in gdb, it says "program exited with code 01"?

A.
Function main() should return 0 to signify the program went fine...If you don't explicitly return 0 from main(), then the return value from the program will be some arbitrary value.

Note that these comments are also relevant to what you provide as an argument to function exit().


Q.
I am trying to use gdb to debug a program that has a segmentation fault. It tells me the problem occurs at:
0xef746954 in memcpy ()

and when I type the Gdb where command it says it cannot access memory at address 0xef7fffb0. What does this means?

A.
It means that the Seg fault doesn't occur directly in one of your functions, but in a library function named memcpy(). Of course, it still means you've done something bad in your code.

First, make sure you compiled your program with the -g option (to get debugging information). Then, if you still cannot get context information (i.e., what piece of your code led to the error) from the where command in Gdb, you'll have to do more work....I.e., either by using printfs, like:

printf("Point A\n");

(don't forget the newline) or by setting breakpoints (in Gdb), narrow down the region where the error occurred until you find the single line where the problem is.

Remember that Segmentation Faults often occur when using a pointer improperly or with arrays (e.g., using an index that is out-of-range).


Q.
Is there any way to check if the correct amount of memory has been deallocated?

A.
You could use a single global int counter variable. Start the variable out at 0. Then wherever you have a malloc, add code to increment the counter. Wherever you have a free, add code to decrement the counter.

Then, print the counter whenever you want to see what it is OR monitor the counter from inside the debugger.

The only thorough method is to keep track of the addresses of things when they are allocated, and make sure all those same addresses are parameters to calls to free at some point.


Make Files

Q.
With a make file, I am getting the error "unexpected end of line seen"?

A.
Are you forgetting to put Tab characters at the beginning of the lines with compilation/linking commands?


BU CAS CS 113 - FAQ - Debugging