== 3139 ==条件跳转或移动取决于未初始化的值

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

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

== 3139 ==通过0x400F25:main(mylib.c:11)

大家好,我仍在尝试将其插入哈希表。我不能完全正常工作,我已经包括了我的打印方法,只是因为我认为这可能是一个问题。我正在尝试进行线性探测。当我运行valgrind时,出现了此错误,我认为这与复制到字符串中有关,但是我不确定是否意味着什么?我现在真的不知道如何使插入工作,一些输入将是很棒的。
哈希表插入中的第56行是strcpy(str,key)

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]);
      }
   }
}

最佳答案

您的strcpy应该是strcpy(key,str),而不是相反。 (您可以只使用strdup,btw并保存malloc + strcpy)。

另外,在:
   if(h-> key [remainder]!= NULL && strcmp(str,h-> key [remainder])!= 0){

条件“ h-> key [remainder]!= NULL”是多余的:您已经在上面进行了检查。

最后,在循环中(遍历存储桶),看起来像:


循环条件应为您应该在某处增加余数,或使用“ remainder + i”和h-> keys的索引
代替“ capacity == i”->“ i = 0”,只需使用“(remainder + i)%capacity”作为h-> keys的索引


..最后,最后-循环之外的第一部分可以在循环中,保存代码。

07-24 09:46
查看更多