Here is my code that creates this problem;
#include <stdint.h>
#define HASHPRINT(STRING) printf(STRING ": %p\n", simple_hash(STRING))
uint32_t simple_hash(const char *string) {
uint32_t hash;
char buffer[4]; /* 4 bytes = 32 bits */
const char *c;
int i = 0;
for(c=string; *c!=0; c++) {
buffer[i] = *c;
i++;
if (i == 3) {
i=0;
printf("\nAdding %u to hash\n", *((uint32_t *)buffer));
hash += *((uint32_t *)buffer);
hash = hash ^ *((uint32_t *)buffer);
}
}
if (i > 0) {
hash += *((uint32_t *)buffer);
hash = hash ^ *((uint32_t *)buffer);
}
return hash;
}
void main() {
HASHPRINT("yasar");
HASHPRINT("rasay");
HASHPRINT("arsay");
HASHPRINT("yasra");
HASHPRINT("osman");
HASHPRINT("ali");
HASHPRINT("veli");
}
Program output changes depending on if I comment out or not the printf function call in line 18.
Without printf, my program outputs this:
yasar: 7D90F834
rasay: 00000005
arsay: 00000003
yasra: 00000001
osman: 00000003
ali: 00000001
veli: 00000005
If I enable the printf function, I get this output instead;
Adding 2004050297 to hash
yasar: 0F2400A0
Adding 7561586 to hash
rasay: 78C921B4
Adding 7565921 to hash
arsay: 78C94C94
Adding 7561593 to hash
yasra: 78C92194
Adding 7172975 to hash
osman: 78DD7FAC
Adding 6909025 to hash
ali: 7842A494
Adding 7103862 to hash
veli: 78C3698C
I would expect that in both cases, calculated hash values (which are printed after word, seperated from word with :
) would be the same.
I was wonderin what would be the cause of this problem.
I am using WinXp with MinGW with gcc 4.8.1 version.