Skip to main content
gamedev.net
🔒 Locked

Weird Bug

Started by Hexus Aug 2, 2006 at 11:03 AM 6 replies 900+ views
Original Post
Hexus
Hexus
The assertion in the following code is failing, but it should be impossible for it to fail:
TerrainNode* Terrain::GetRightNeighbor(TerrainNode *node)
{
	uint size = 1 << node->level;
	if(node->x + size >= MaxValue)
	{
		switch(node->face)
		{
			switch(node->face)
			{
			case FaceFront:
				return Traverse(&_rootNodes[FaceRight], 0, node->y, node->level);
			case FaceRight:
				return Traverse(&_rootNodes[FaceBack], 0, node->y, node->level);
			case FaceLeft:
				return Traverse(&_rootNodes[FaceFront], 0, node->y, node->level);
			case FaceBack:
				return Traverse(&_rootNodes[FaceLeft], 0, node->y, node->level);
			case FaceTop:
				return Traverse(&_rootNodes[FaceRight], MaxValue - size - node->y, 0, node->level);
			case FaceBottom:
				return Traverse(&_rootNodes[FaceRight], node->y, MaxValue - 1, node->level);
			}
		}
	}
	assert(node->x + size < MaxValue);
	return Traverse(GetCommonAncestor(node, node->x ^ (node->x + size)), node->x + size, node->y, node->level);
}
I've checked to make sure that it's not falling through the switch, so that is not the problem. I just can't imagine how this assertion could ever fail. The value of node->x is 0x07800000, size is 0x00800000, and MaxValue is 0x08000000. node->x + size is equal to MaxValue, so expression in the if statement should evaluate to true. Any ideas?
smart_idiot
smart_idiot
Why do you have switch(node->face) twice? Also, add a default statement and add 'assert(false);' in there, maybe node->face is an impossible value.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Hexus
Hexus
Quote:
Original post by smart_idiot
Why do you have switch(node->face) twice? Also, add a default statement and add 'assert(false);' in there, maybe node->face is an impossible value.


Wow, I must be blind. Thanks for pointing that out. I did the default case and assert false before, but only on the inside switch. Oops. Shouldn't there be a syntax error considering the nested switch doesn't have a case?
smart_idiot
smart_idiot
I just ran it through my compiler. Amazingly, it did compile it. I would have thought that wasn't valid syntax.

It did give me a warning, though: 'warning: unreachable code at beginning of switch statement'. I wub my compiler.

So, apparently, this is somehow valid but it's effectively a nop and the code gets ignored. Try enabling some warnings. Warnings are good.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
kvp
kvp
Quote:
Original post by smart_idiot
I just ran it through my compiler. Amazingly, it did compile it. I would have thought that wasn't valid syntax.

It did give me a warning, though: 'warning: unreachable code at beginning of switch statement'. I wub my compiler.

So, apparently, this is somehow valid but it's effectively a nop and the code gets ignored. Try enabling some warnings. Warnings are good.


A switch statement should compile as a table or logic based goto, that has a target scope of 1 statement after the switch. The break-s are needed because they act as goto-s to the automatic after_the_switch label for the compiler. The whole c syntax must be translatable to single expression if/goto pairs and lots of generated labels. This logic is what powers Duff's Device (check wikipedia for the code).

Viktor
Deyja
Deyja
Quote:
node->x + size is equal to MaxValue, so expression in the if statement should evaluate to true.
Why would it be true? It's clearly false. You are asking 'Is this plus that less than something else'. This plus that is not less than something else; it is equal to something else!
Hexus
Hexus
Quote:
Original post by Deyja
Quote:
node->x + size is equal to MaxValue, so expression in the if statement should evaluate to true.
Why would it be true? It's clearly false. You are asking 'Is this plus that less than something else'. This plus that is not less than something else; it is equal to something else!


I was referring to the if statement at the top of the method, which is asking if it greater or equal than, not less than. The assertion should have never been reached if it weren't for the nested switch statements.
Deyja
Deyja
You described the case in which the assertion fails; then claimed it shouldn't fail. You'll excuse me if I read what you typed, and not what you meant.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.