I'm working with hash tables and I came across this function. But what does hash / sizeof(void *) mean? and the comment given after it - get rid of known-0 bits?
// This matches when the hashtable key is a pointer.
template<class HashKey> class hash_munger<HashKey*> {
public:
static size_t MungedHash(size_t hash) {
// TODO(csilvers): consider rotating instead:
// static const int shift = (sizeof(void *) == 4) ? 2 : 3;
// return (hash << (sizeof(hash) * 8) - shift)) | (hash >> shift);
// This matters if we ever change sparse/dense_hash_* to compare
// hashes before comparing actual values. It's speedy on x86.
return hash / sizeof(void*); // get rid of known-0 bits
}
};
sizeof(void) will not compile on a C compiler. The void type comprises an empty set of values; it is an incomplete object type that cannot be completed. the answer is 0(zero).
The size of a void pointer is different in different systems. In 16-bit systems, the size of a void pointer is 2 bytes. In a 32-bit system, the size of a void pointer is 4 bytes. And, in a 64-bit system, the size of a void pointer is 8 bytes.
The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.
On a 32-bit arch, sizeof(void *) is 4 bytes, so p++, would be set accordingly.
On most machines and ABIs, pointers are generally word-aligned. So dividing by the sizeof
pointers is essentially ignoring the least bits (e.g. 3 lowest bits of bytes addresses are 0 on most 64 bits processors since a 64 bits word has 8 bytes, each of 8 bits).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With