c++hashmurmurhash

C++ What should we pass in MurmurHash3 parameters?


I am confused with what parameter should I provide for the MurmurHash3_x86_128(). The murmurhash3 code can be found https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp. Method definition is given below.

void MurmurHash3_x86_128 ( const void * key, const int len,
                       uint32_t seed, void * out )

I have passed the following values in the above method but my compiler is giving me segmentation fault. What am i doing wrong ?

int main()
{
    uint64_t seed = 1;
    uint64_t *hash_otpt;
    const char *key = "hi";
    MurmurHash3_x64_128(key, (uint64_t)strlen(key), seed, hash_otpt);
    cout << "hashed" << hash_otpt << endl;
    return 0;
}

Solution

  • This function put its hash in 128 bits of memory.

    What your are doing is passing a pointer, that is not allocated yet to it.

    The correct usage would be something like that:

    int main()
    {
       uint64_t seed = 1;
       uint64_t hash_otpt[2];  // allocate 128 bits
       const char *key = "hi";
       MurmurHash3_x64_128(key, (uint64_t)strlen(key), seed, hash_otpt);
       cout << "hashed" << hash_otpt[0] << hash_otpt[1] << endl;
       return 0;
     }
    

    You could have noticed that by analyzing how MurmurHash3_x86_128 fills out parameter:

    ((uint64_t*)out)[0] = h1;
    ((uint64_t*)out)[1] = h2;