• Welcome to Valhalla Legends Archive.
 

What's wrong with this code? (Or: ALWAYS compile with -Wall.)

Started by Yoni, July 29, 2005, 03:32 PM

Previous topic - Next topic

Yoni

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 ...


Mangix


Newby

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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

Yoni

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.

Newby

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 */ :)
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.


Warrior

Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

R.a.B.B.i.T


Yoni

It's a spoiler blocker. If you don't want to see the answer you don't have to.

Soul Taker

I think they're using a different theme, since not all themes have black quotes.