0

I compiled the program with -Criot -gl flags and instead of 1 I get a lot of results to my surpise (in fact, I was looking for fix a 216 error). The first is with the below code that's a simple hashing function. I have no idea how to fix this.

function HashStr(s : string) : integer;
var h : integer;
var c : char;
begin
   h := 0;
   for c in s do
      h := ord(c) + 31 * h; { This is the line of error }
   HashStr := h;
end;

How can this be out of ranges?

The Mask
  • 17,007
  • 37
  • 111
  • 185

1 Answers1

2

Easily, say you have a string "zzzzzzzzzzz". Ord(c) wil be 122, so the sequence is

 H = 122 + (31* 0 ) = 122
 H = 122 +(31*122) = 3902
 H = 122 +(31*3902) = 121146

Which exceeds the 32767 limit for 16 bit ints, if it's a 32 but int, it won't take many more iterations to exceed that limit.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • Thanks. I've fixed that. But now I got an error 215. I'm already using `Cardinal` type and the string aren't greater than 12 characters. How can I fix this? – The Mask Dec 20 '13 at 23:36
  • 1
    Use qword and you are good for 64-bit. But that is only postponing the inevitable. Explain more about what you try to accomplish. – Marco van de Voort Dec 22 '13 at 13:37