I guess I'm a C++ novice. I'm certainly not a C++ expert. Every now and again I figure out a problem that makes me stop and wonder how I never learned this before. (Or possibly, why I didn't remember this when I learned it before.)
I'm take a graph theory class. It's been a lot of fun. Graph theory really isn't too difficult so long as you aren't afraid of lots of pointers and dynamic memory allocation. For homework, I had the option of using C or C++, so I chose C++ to keep everything beautiful. Well, almost beautiful. This is C++, after all.
I was plugging away on my homework when I got one of those dreaded "segfaults". As per my usual habit of diagnosing pointer problems, I started littering my code with asserts and then used ddd (my favorite debugger GUI) to see what's going on when the asserts fail. In the process, I discovered a better way...at least for memory and pointer issues: Electric Fence and valgrind.
Electric fence helps you find memory problems by stopping the program in it's tracks when pointers and deallocation run amuck--as opposed to letting the program run just fine until you get "lucky" and a pointer finally does something bad enough to bring the program to a screeching halt. It's one of those tools I'd heard about and never got around to using because I thought it would be complicated. Ha! It's not even close to complicated. To use it, simply compile like so "gcc -o my_prog my_prog.cpp -u malloc -lefence" and run your program. Note how electric fence replaces the malloc library.
Valgrind gives you a running tally of the cumulative evil of your program and halts the program if necessary. I found valgrind more helpful than electric fence because of it's verboseness. (Electric fence gives you about as much information as "your program is bad, fix it.") Another plus for valgrind is that you don't have to link against it, you just have valgrind launch your program like so: "valgrind --tool=memcheck --leak-check=yes my_prog".
In the end, what was my problem? The first problem I had was not initializing a pointer to NULL and then assuming it was NULL later on (oops). My second problem was that I was calling "delete" (delete a single element) instead of "delete[]" (delete an array of elements). How did I not learn about "delete[]" before? I suppose it does look vauguely familiar. I guess *I'm* losing my memory now. (Yeah, yeah, bad pun.)
Now I know even *more* about C++. Mu-ha-ha-ha-ha!
Posted by enigma at January 28, 2005 11:30 AMYeah, I'm a little scared that I got through college without learning the delete/delete[] distinction.... no very scared about that actually. I don't even remember seeing it until I picked up "Effective C++" (excellent book, btw, if you're doing a lot of practical work in C++).
The most frustrating bug I ran into in recent memory was when I was getting memory overwrites when assigning values into an array... basically writing into the area *after* the array. Something really really simple giving me really nasty program results. I reviewed the code a dozen times, walked through it with a debugger over and over again. I checked an rechecked the struct definition and I noticed something that I'd missed the first dozen times... the array size was 0. I was a bit peeved at myself for 1) making the mistake in the first place and 2) not catching the problem from the behavior, which seems obvious in hindsight. Most of all I was peeved 'cuz I know I'm capable of writing *much* better bugs than that one--and I have witnesses who'll back me up on that.
As for valgrind, my experience with it is limited because when I tried it, it was unusably slow. The impression I got was that it wasn't useful for anything but the most trivial cases. Perhaps I needed to disable some extra checks or the program I was running it on was just a particular case that is problematic for it.
Posted by: Kevin Worcester at January 28, 2005 01:58 PMI used to use C++ all the time... I loved managing memory myself... I even refused to use the STL.
For time critical or precise tasks, I suppose it remains the ultimate of control, but I prefer more reasonable languages where such concerns are automatically handled.
Although, if stuck between C or C++, I'd choose C++ too.
Posted by: Steve at February 23, 2005 03:01 PM