Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 10:58 PM   #1
pulpfiction
Newbie
 
Join Date: Mar 2006
Posts: 1
Rep Power: 0 pulpfiction is on a distinguished road
Strange behavior: getting different results from the same input on a function. WTH?

I'm getting a strange behavior on this little script.

I'm trying to implement a hash table, so I have a hash function which should always return the same numeric result for a given string.

Problem is, depending on the parent function which is calling hash(), it returns different results for the same string. If I call it twice inside the same function it works ok.

Now the really strange thing (well, I don't know, maybe I'm doing something wrong - I'm new to C) is this behavior depends on the machine I'm running the script. At university machines, running Windows, I got the error. Then I tried at home on Windows and Linux, same thing. I then asked for a friend to try for me... it worked!

http://francisco.datazero.net/myhash_fail.jpg
http://francisco.datazero.net/myhash_ok.jpg

You're encouraged to test it on your machine too to see if I'm going crazy.

This is the code (quite simple script):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HASHELEMENTS 100
#define MAXLEN 100

struct htab {
    struct htab *child;
    struct htab *parent;
    unsigned int key;
    char value[MAXLEN];
};

struct htab *hashtab[HASHELEMENTS];

unsigned int hash (char *string) {
    unsigned int hashval;

    while (*string != '\0')
        hashval += *string++;

    return hashval % HASHELEMENTS;
}

// temporary debug functions
int testhash1(char *test1) {
    unsigned int hashval;

    return hash(test1);
}
int testhash2(char *test2) {
    unsigned int hashval;

    return hash(test2);
}

int main(){
    char string[MAXLEN];
    unsigned int hashval;
    struct htab *curhash;

    do {
        printf("enter value to hash: ");
        scanf("%s",&string);
        printf("hash1: %d\n",testhash1(string));
        printf("hash2: %d\n",testhash2(string));
    } while (1); // use ctrl+c to exit
}

Does anyone have any idea what's happening?
pulpfiction is offline   Reply With Quote
Old Mar 28th, 2006, 11:43 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
unsigned int hash (char *string) {
    unsigned int hashval = 0;

    while (*string != '\0')
        hashval += *string++;

    return hashval % HASHELEMENTS;
}

You have an uninitialised variable. The value that hashval starts with depends on what is currently in memory. Add in the code I put in red, above and you should be fine.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC