# Error handling in a size_t hashfunction

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.

• I'm not sure this question is appropriate here. – Yuval Filmus Apr 22 at 10:19
• If I remember correctly, size_t is unsigned, so it can't be negative. – Yuval Filmus Apr 22 at 10:19
• 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. – Yuval Filmus Apr 22 at 10:20
• This sounds more like a software engineering question to me. – Yuval Filmus Apr 22 at 10:21

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