0
$\begingroup$

For school I have a big assignment on hashtables etc. One of the functions is a size_t function where we calculate the hash value. (Keep in mind the structure of every function is given so we are not allowed to change input variables/function types). However, the hash calculator function does need to handle errors, but I am stuck on what value to return when encountering errors. In my head: all positive numbers are also possible hash values, and all negative numbers can not be returned in a size_t function.

Thanks in advance

$\endgroup$
  • $\begingroup$ I'm not sure this question is appropriate here. $\endgroup$ – Yuval Filmus Apr 22 at 10:19
  • 1
    $\begingroup$ If I remember correctly, size_t is unsigned, so it can't be negative. $\endgroup$ – Yuval Filmus Apr 22 at 10:19
  • 1
    $\begingroup$ What would it mean for the hash function to handle errors in your case? I have no idea. Abstractly, a hash function gets an object and outputs a hash value, and the most important property is that if you give it the same object then it should output the same value. If the input is "malformed", the output is really undefined. You could perhaps throw an exception, but that's really up to you. $\endgroup$ – Yuval Filmus Apr 22 at 10:20
  • 1
    $\begingroup$ This sounds more like a software engineering question to me. $\endgroup$ – Yuval Filmus Apr 22 at 10:21
1
$\begingroup$

I have never seen anyone doing error handling in the calculation of a hash function.

For a hash table to work, items must be equatable and hashable. If they are not equatable then sorry, you lost. As long as you can detect that an item is “bad” for equatability, you can just make it compare different to any item other than itself. And make it produce a hash code if constant zero or any other constant.

If you have problems calculating a hash function then you have a bigger problem that you need to solve first.

| cite | improve this answer | |
$\endgroup$
0
$\begingroup$

Since you cannot change the signature of the function, if you really want to get the error code then create a global variable int error_hash_compute, and whenever you call this particular function, before using its return value, check the value of error_hash_compute. In the hash compute function, before returning you can set this variable value to appropriate error code (0 for success, for example).

Also, as @YuvalFilmus wrote in a comment, it's not clear what kind of errors are you talking about... I think you could explain this in detail in your question.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.