Is it possible to safely assume that the 16 high bits (2 are sufficient for me) in a 64-bit pointer are unset?

StackOverflow https://stackoverflow.com/questions/13795108

Domanda

I'm building a data structure which sets into 64 bit integers a set bit every 2 or 3 bits.

It would be convenient for me if I could alternately also store a pointer in this structure (when they are terminal nodes in a tree, but that's not relevant).

For the purposes of my representation (which guarantees either the highest or second highest bit are set), if it can be assumed that pointers never have their highest two bits set, i.e. this assertion holds:

void *sixty_four_bit_pointer = a_valid_address(); 
bool always_zero = 0xC000000000000000 & sixty_four_bit_pointer;

then I can do this trick!

È stato utile?

Soluzione

This is not safe in general. It's programs that pull tricks like this that led to the /LARGEADDRESSAWARE flag in Windows. (Aka "this is why we can't have nice things.")

What you can do is use the bottom bits instead of the top bits. Since your structure contains a pointer, it is already 8-byte aligned, which means that the bottom three bits are always 0, so you can use those bits as tag bits.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top