Comp 210 Lab 12: C Pitfalls
Table of Contents
This lab will discuss some common C pitfalls.
You can also browse the
C FAQ
from comp.lang.c.
Disclaimer: some of the constructs advised against here are
occaisionally justifiable.
Our warnings are only guidlines, but
don't ignore them unless you know what you're doing.
Exercise
/*
* Copy this program, which can be found in
* ~comp210/Labs/lab12/wrongo.c,
* and compile it to see how well the compiler likes it.
* The file has a number of (hopefully) fairly obvious mistakes,
* as well as some hideous indentation.
* Modify it, so that it shines.
*/
#include <stddef.h> // get declaration of NULL
#include <stdio.h> // get declaration of printf
int main() {
double x;
printf( "Enter a positive x, and I'll calculate how many doublings\n" );
printf( "of 1 it takes until x is met or exceeded: " );
scanf( "%lf", &x );
int n = 1,doublings=0;
for ( bool notDoneYet = x > 0; notDoneYet = true; notDoneYet == n >= x );
{
n == n * 2;
doublings++;
}
printf( "ceil(log_2(%g)) = %d.\n", x, doublings );
return 0;
}
(The program correctly uses scanf
to read a double.)
When you're done polishing, you may want to compare
with righto.c.
When you compile and run your program, the most dreaded error is
"Segmentation fault".
(At least that's what gets printed in UNIX;
under MacOS and Windows it is pronounced "application unexpectedly quit"
or "bomb icon" or "system frozen".)
This usually indicates that your program is trying to read or write
to an illegal memory location
(either because your program doesn't have permission to that part of
memory (UNIX),
or that location doesn't actually exist (UNIX or PC-OS)).
When I see a segmentation fault, my first suspicions are that my code is
The first, we've warned you about enough times.
Here are some examples of the second, uninitialized/null pointers:
char str1[200] = "hi"; // This is fine.
char *str2; // str2 has no value yet; okay but be cautious.
strcpy( str2, "Katasrophe" ); // WRONG we're copying into str2 which
// could be pointing at anywhere in memory.
// "str2 = str1" would have saved us,
// by having str2 and str1 both point
// at a (valid) block of 200*sizeof(char)
Here's another example, which uses a structure as briefly talked about in
one lecture:
#include <stddef.h> // Get declaration of NULL.
#include <stdio.h> // Get declaration of printf.
struct intCons { // Declare a type "struct intCons"
int car; // with a car and a cdr.
struct intCons *cdr;
}
intCons* l2 = new intCons;
l2->car = 5;
l2->cdr = NULL;
intCons* l1;
printf( "%d", l2->car ); // Fine, prints 5.
l1 = l2->cdr; // Fine, l1 gets NULL.
if (l2->cdr->cdr == NULL) // WRONG: l2->cdr is NULL, so 'the thing
// pointed to by l2->cdr' makes no sense.
printf( "l2 has only one item.\n" );
// Compare with also-incorrect Scheme:
// (if (null? (cdr (cdr l2))) ...)
A Bus Error is (approximately) as frightening as a Segmentation Fault.
If you see "Bus Error", look for the same
two type of problems as might cause a seg fault.
(Advanced:)
Here is one way a bus error can occur, for those who care:
On some machines, when the women in the controller ask for the
contents of a memory bucket/byte, they actually get
several (perhaps 4) buckets, a "word".
All good and fine, except that when requesting a memory location,
the request must be a multiple of 4 ("on a word boundary").
That is,
not every memory-location (bucket) can be fetched individually
(it's not "byte-addressable").
A bus error occurs if a bucket is requested that isn't a multiple of 4.
Now usually the C compiler takes care of this for you (after all,
this word size, 4, varies from machine to machine, being 1 in the trivial
case).
However, if you're accessing through a garbage pointer, then
it may not be pointing at a word boundary, and the memory doesn't
know what values to ship to the women via the data bus.
Common C Error Messages
Although you are using (extended) C,
this list of
common C++ errors
also includes errors which you'll encounter from the C compiler.
(Just ignore the C++-specific items, which talk about
"class", "constructor", "object", "instance", or "method".)
Back to Comp 210 Home