[prog] Segmentation fault :-(

Kathryn Hogg kjh at flyballdogs.com
Tue Sep 24 10:49:50 EST 2002


> assert.h provides the assert function.  If the test is false it'll kill
> your program and provide you a useful error message.  I found it a very
> handy debugging technique.  Much tidier than:
> if(data == NULL)
> {
>      fprintf(STDERR, "data is NULL at line 1234\n";
>      exit(1);
> }
> especially because it saves you from worrying about moving line numbers.
> ;)

I use assert all the time but you don't have to worry about line numbers
and such in IO statements since the preprocessor gives us the handy
__FILE__ and __LINE__ macros:

   cerr << __FILE__ << " line " << __LINE__ << ": data is 0" << endl;

If you are using C++, start using iostreams instead of printf.  Much, much
better.


-- 
Kathryn





More information about the Programming mailing list