Code: Select all
new_direction = compare_avl_node( node - num_parameters_compare,
root->left - num_parameters_compare);
The problem is, the AVL tree code does not know of any container structures. I still think you should pass avl_node* pointers to the comparison function, and extract the address of the container structure there, like this:
Code: Select all
int avl_compare(avl_node* a, avl_node* b)
{
mem_block* left = (mem_block*)((uint8*)a - &((mem_block*)NULL)->node);
mem_block* rigth = ...;
return (left->address < right->address) ? 1 : 0; // or whatever the actual comparison may be.
}
The pointer magic may seem difficult at first, but it can be tucked away into a macro. What this does is the following:
- takes the address of the avl_node structure (a)
- converts it into a byte pointer (uint8*)
- converts the value 0 into an pointer of type mem_block
- takes the address of the field 'node' in that structure. using the fact that the pointer points to virtual address zero, this returns the byte offset of the said field from the start of the structure.
- and subtracts the offset from the byte pointer, effectively moving the byte pointer to the start of the structure
- now the pointer is converted back to the correct type.
Or, if this seems complicated, you can cast the node pointers to uint8*, and subtract the offset manually. However,
another issue with a solution like
Code: Select all
unsigned int address = *(node - 2);