Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Yoni on July 29, 2005, 03:32 PM

Title: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Yoni on July 29, 2005, 03:32 PM
Here is some code I wrote today for practice.
This code has a terrible, evil bug that causes it to seg-fault.
Can you spot the bug?

(Note: Compiling it = cheating. Compilation with -Wall reveals the answer.)


... irrelevant code snipped ...

bool HeapEmpty()
{
return HeapSize == 0;
}

void HandleGroup(uint i)
{
// Is the heap empty?
if (HeapEmpty()) {
// Nothing to do with this group =\
return;
}

// Get the currently valid trip that has the least chance of attracting more groups.
uint TIdx = HeapExtractMin();

// Match!
NumMatched++;
Match[Arr[i].Index + 1] = TIdx + 1;
}

... irrelevant code snipped ...

Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Mangix on July 29, 2005, 03:59 PM
either the uint thing or something else :(.
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Newby on July 29, 2005, 04:49 PM
It's never going to return, even if the heap is empty.

QuoteChange "=\" to "=/" is my best guess. It seems you commented out return on accident.

The only reason I know this is because I have done the same thing (though it would seem that my mistake (forgetting to add to a number) is less severe than yours). Yay for syntax highlighting! I've never coded in pico since. vim for life.
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Yoni on July 29, 2005, 05:12 PM
Newby rocks.

QuoteActually, it might return, just not from the "return;" line. :)
But, as soon as we hit HeapExtractMin(), it tries to extract from an empty heap, which sometimes crashes and otherwise returns junk.
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Newby on July 29, 2005, 06:04 PM
Quote from: Yoni on July 29, 2005, 05:12 PM
Newby rocks.

QuoteActually, it might return, just not from the "return;" line. :)
But, as soon as we hit HeapExtractMin(), it tries to extract from an empty heap, which sometimes crashes and otherwise returns junk.

:)

QuoteYeah, I guess I should have specified that it won't return from that return line if no heap exists.

That's also a reason why I use /* code blocks */ :)
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Yoni on July 30, 2005, 12:24 AM
:)
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Warrior on July 30, 2005, 12:43 PM
Why do you talk like this?
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: R.a.B.B.i.T on July 30, 2005, 01:47 PM
Cause they want to look cool
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Yoni on July 31, 2005, 03:22 PM
It's a spoiler blocker. If you don't want to see the answer you don't have to.
Title: Re: What's wrong with this code? (Or: ALWAYS compile with -Wall.)
Post by: Soul Taker on July 31, 2005, 06:23 PM
I think they're using a different theme, since not all themes have black quotes.