Lessons learnt, it's high time i should start using debugger, no more cout, printf.
Partly right, partly wrong. The right part is is that you shouldn't use cout or printf. The wrong part is that you should use a debugger instead. Use cerr or fprintf(cerr, ...). The best use of a debugger is for locating the point of a segfault:
$ gdb ./my_exe core (gdb) bt For string conversions,
Never underestimate the power of print statements for debugging - The elements of programming style.
Written by the Gods themselves. Use a debugger, but never stop using print statements for debugging. Also consider using cerr or fprintf(stderr, ...) instead of cout and printf. It's easier to separate your debugging output from regular output using redirection:
Comments 3
Partly right, partly wrong. The right part is is that you shouldn't use cout or printf. The wrong part is that you should use a debugger instead. Use cerr or fprintf(cerr, ...). The best use of a debugger is for locating the point of a segfault:
$ gdb ./my_exe core
(gdb) bt
For string conversions,
string str;
char * foo = const_cast(str.c_str()); // Safe, since c_str() returns a copy
const char * bar = str.c_str();
Also see this: http://www.awprofessional.com/articles/article.asp?p=21853
Reply
char dest[BUFSIZ];
string source = "foo";
strncpy(dest, source.c_str(), BUFSIZ);
Reply
- The elements of programming style.
Written by the Gods themselves.
Use a debugger, but never stop using print statements for debugging. Also consider using cerr or fprintf(stderr, ...) instead of cout and printf. It's easier to separate your debugging output from regular output using redirection:
$ foo 2>debug.log
will direct your debugging output to debug.log
- Philip (former PCCPfac)
Reply
Leave a comment