2012-08-30 57 views
0

== 3139 ==條件跳轉或移動取決於未初始化值(S)線性探測哈希表插入

== 3139 ==在0x4A0673F:的strcpy(mc_replace_strmem.c:311)

== 3139 ==由0x400ADB:htable_insert(hashtable.c:56)

== == 3139通過0x400F25:主(mylib.c:11)

大家好,我仍然試圖插入到一個哈希表。我無法完成它的工作,我已經包含了我的打印方法,只是因爲我認爲這可能是一個問題。我試圖做線性探測。當我跑valgrind時,我得到了這個錯誤,我認爲它與複製到我的字符串有關,但我不確定是什麼意思?我真的不此時知道如何得到這個工作的插入,一些輸入將是美好的...
線56在哈希表中插入是的strcpy(STR,鍵)

int htable_insert(htable h, char *str) { 
    int i; 
    /*convert string to integer*/ 
    unsigned int index = htable_word_to_int(str); 
    /*calculate index to insert into hash table*/ 
    int remainder = index%h->capacity; 
    /*once calculated position in the hash table, 3 possibilities occur*/ 
    /*no string in this positon, copy string to that position, increment number of keys, return 1*/ 
    if (h->key[remainder] == NULL) { 
     char *key = emalloc(strlen(str) + 1); 
     strcpy(str, key); 
     h->key[remainder] = key; 
     h->frequencies[remainder] = 1; 
     h->num_keys++; 
     return 1; 
    } 
    /*the exact same string is at the position, increment frequency at that position, return frequency*/ 
    if (strcmp(str, h->key[remainder]) == 0) { 
     h->frequencies[remainder]++; 
     return h->frequencies[remainder]; 
    }/*a string is at that position, but it isnt the rightone, keep moving along the array 
     until you find either an open space or the string you are looking for*/ 
    if (h->key[remainder] != NULL && strcmp(str, h->key[remainder]) != 0) { 
     /*you may need to wrap back around to the beginning of the table, so each time you add 
     to the position you should also mod by the table capacity.*/ 
     for (i = 0; i <= h->capacity; i++) { 
     /*no string in this positon, copy string to that position, increment number of keys*/ 
     if (h->key[remainder] == NULL) { 
      char *key = emalloc(strlen(str) + 1); 
      strcpy(str, key); 
      h->key[remainder] = key; 
      h->frequencies[remainder] = 1; 
      h->num_keys++; 
     } 
     /*if you find the string you were looking for, increment the frequecny at the position 
      and return the frequency*/ 
     if (strcmp(str, h->key[remainder]) == 0) { 
      h->frequencies[remainder]++; 
      return h->frequencies[remainder]; 
     } 
     if (h->key[remainder] != NULL && h->capacity == i) { 
      i = 0; 
     } 
     } 
    } 
    /*if you have kept looking for an open space but there isnt one, the hash table must fu*/ 
    return 0; 
} 

void htable_print(htable h, FILE *stream) { 
    int i; 
    for(i = 0; i < h->capacity; i++) { 
     if(h->key[i] != NULL) { 
     fprintf(stream, "%d%s\n", h->frequencies[i], h->key[i]); 
     } 
    } 
} 

回答

1

你的strcpy的應該是的strcpy(鍵,str),而不是相反。 (你可以使用strdup,順便說一句,並保存malloc + strcpy)。

另外,在: 如果(!H->鍵[其餘] = NULL & &的strcmp(STR,H->鍵[餘數)= 0){

條件「H->關鍵[餘數]!= NULL「是多餘的:你已經檢查過了。

最後,在你的循環(打算在桶),這似乎是:

  1. 循環條件應該是<,不< =
  2. 你應該某處遞增餘,或使用「餘+ i「和索引到h->鍵
  3. 而不是」capacity == i「 - >」i = 0「,只需使用」(餘數+ i)%容量「作爲h->鍵的索引

..最後最後 - 初始部分,即循環外部的部分,可以在循環中,保存代碼。

+0

好吧,所以我改變了循環條件和複製的東西。你在2和3說的話讓我有點困惑。 – courtney

+0

但是,謝謝你,你確實解決了我的問題:) – courtney

+0

「條件」h->鍵[餘數]!= NULL「是多餘的」 - ** **條件都是多餘的。 –