Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LONGLONGSIZE (sizeof (unsigned long long))
union intstr {
unsigned long long ikey;
char skey[LONGLONGSIZE];
};
unsigned hashShortString (char v[], unsigned M)
{
union intstr *is;
is = (union intstr *) v;
return is->ikey % M;
}
int main (int argc, char *argv[])
{
char key[LONGLONGSIZE];
if (2 != argc) {
printf ("USAGE: hash_short STRING\n");
printf (" (STRING will be truncated after %u characters)\n",
LONGLONGSIZE *LONGSIZE);
return EXIT_FAILURE;
}
strncpy (key, argv[1], LONGLONGSIZE * LONGSIZE);
printf ("Hash: %u\n", hashShortString (key, 8191));
return EXIT_SUCCESS;
}
This is my lecturer's code for hashing short strings, I'm not entirely sure why this can't take more than 8 characters, I'm guessing because it's in integer form? Any hints?
The stuff in red is what I tried doing, it says the string will terminate after 64 characters but gives a segfault after I enter something in.