Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does dividing by sizeof(void *) mean?

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
        }
      };
like image 912
annunarcist Avatar asked Oct 24 '13 13:10

annunarcist


People also ask

What does sizeof void * return?

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).

What is the size of a void *?

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.

What is size of void in bytes * 1?

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.

What is size of void in bytes in Java?

On a 32-bit arch, sizeof(void *) is 4 bytes, so p++, would be set accordingly.


1 Answers

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).

like image 84
Basile Starynkevitch Avatar answered Oct 04 '22 09:10

Basile Starynkevitch