Carl Cerecke wrote:

> And always use the -Wall flag to gcc. It would have warned you about it.

Are you sure?

It may have warned about the incorrect 'if' statement, but this would not 
have caused a segfault.  I've made this mistake many times :(

The runtime problem would have been caused by calling fgets() with a non-
existent buffer.  C has no inherent bounds-checking for buffers so there 
is no way that fgets() can know whether the pointer you pass it actually 
points to valid allocated memory.  GCC won't warn you about this as 
fgets() expects a 'char *' which is exactly what it's getting.

Out of curiosity I just tried compiling the original code with -Wall:
dave @ smurf:~/temp> gcc -Wall quote.cpp -o quote
quote.cpp: In function `main':
quote.cpp:9: warning: suggest parentheses around assignment used as truth 
value

so it did pick up the if() :)

(Note: I had to move the declaration of sQuote to the start of the 
function as it complained about it being an undeclared variable)

Cheers,

- Dave

http://www.digistar.com/~dmann/


Reply via email to