more fun with c++ and hilberts curve

TeeJay on 2004-06-11T08:18:24

I just spent 45 minutes in gdb trying to figure out why my C++ program was repeatedly segfaulting, after adding breakpoints, stepping through, and adding lots of cout statements printing all the values I finally discovered that where I expected calls to the getCoordinateFromPoint(int point) method I was geting $BIG_NEGATIVE_NUM rather than 1 to 4.

Suspicious - looks like an unitiated value, having use'd strict in perl since the second week, I thought for a moment and looked at the code.. oh dear.. while refactoring I had removed the code using point, but not its declaration, so while I looped through 1 to 4 using point rather than i, the poor code was being asked to look at element $BIG_NEGATIVE_NUM deep inside a dynamic array.

Even using strict wouldn't have caught that, in C and C++ I must remember to initialise all values to 0 when declaring them unless I want to pass around random junk when I use the wrong variable, easier to trace that than segfaults.

Anyway, just before getting off the train I managed to run a test program using the class and killed my machine twice in a row because the code went into a fast infinate loop. I managed to fix that but there are still subtle bugs in the code so that it doesn't quite work for squares larger than 2 x 2. The new code can be found here.

Hopefully on my next train journey I can fix that and add some output via GD.

I would be very grateful if anybody can point me at the equivilent of Test::Simple or Test::More for C++.


-Wall

djberg96 on 2004-06-11T16:48:56

No, not Larry. Always use the -Wall (or -Wall -W) switches, assuming gcc/g++. Of course, maybe you were and it didn't help in this case (?).

Re:-Wall

TeeJay on 2004-06-11T18:34:12

nope - wasn't using -Wall

I will start, I guess it sets warning level to all, this is probably helpful when debugging :)