here base->left is NULL, and base->right->height is 0:
((((base->left ? base->left->height : -1)) > ((base->right ? base->right->height : -1)))
?
((base->left ? base->left->height : -1) + 1)
:
((base->right ? base->right->height : -1) + 1))
IMO the result of the above expression should be 1,
but it turns out it’s 0 when I print it out.
Anyone knows the reason?
Is it a bug of gcc 4.3.2?
UPDATE
how the expression come?
#define MAX_PLUS_1(a, b) (((a) > (b)) ? (a + 1) : (b + 1))
#define BINARY_TREE_HEIGHT(node) (node ? node->height : -1)
#define BINARY_TREE_SYN_HEIGHT(left, right) \
MAX_PLUS_1(\
BINARY_TREE_HEIGHT(left),\
BINARY_TREE_HEIGHT(right)\
)
it’s really BINARY_TREE_SYN_HEIGHT(base->left, base->right)
And the problem is gone if I replace MAX_PLUS_1 with function:
int MAX_PLUS_1(int a, int b){
return (((a) > (b)) ? (a + 1) : (b + 1));
}
From additional information in our Comments discussion, your
heightfield is unsigned. That means that when you try to compare it to -1 you end up in trouble –(uint32_t)-1 == 0xFFFF_FFFF= Very Big Number, so the ‘left’ branch gets chosen over the ‘right’ branch at some point when you don’t expect it to.Writing
MAX_PLUS_1()as a function solves, or perhaps rather hides, this problem because you useintfor the parameters. That means the comparison is done between theheightvalue and(int)-1, which is what you wanted.