gpt4 book ai didi

c - 链表::哈希表::Valgrind错误::条件跳转或移动取决于未初始化的值

转载 作者:行者123 更新时间:2023-11-30 17:18:24 24 4
gpt4 key购买 nike

再次需要帮助。

我的链接列表可以正常工作。它将文件中的单词列表复制到链接列表中。

现在我想制作一个哈希表,以便所有以字母“A”开头的单词都进入链表的桶[0]中。

我写的代码;它似乎适用于小型和大型列表,但 Valgrind 显示了错误点。

-----CODE-----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct node //struct for linked list
{
char* word;
struct node* next;
}
node;

int findhash(char firstletter) //this function returns a hash value for first alphabet of every word
{
int hash = 0;

if(isupper(firstletter))
hash = firstletter-'A';
else hash = firstletter-'a';

return hash;
}

int main (void)
{
char* dictfile = "small";

FILE* dict = fopen(dictfile, "r");

if (dict == NULL)
return 1;

char oneword[47]; //to store the word from fscanf()

node* hashtable [30]; //creates a hashtable

for (int i = 0; i < 30; i++) //gives an index to every element in the hash table
{
node* temp = (node*)malloc(sizeof(node));
temp->next = NULL;
hashtable[i] = temp;
}

while ((fscanf (dict, "%s", oneword)) > 0)
{
node* temp = (node*)malloc(sizeof(node));
char* tempword = (char*)malloc(strlen(oneword)+1); //gives me a new pointer to store the string (as pointed out by Antione)
strcpy(tempword, oneword);

char firstletter = tempword[0];

int hash = 0;
hash = findhash(firstletter); //returns an index for the first alphabet of the word
temp->word = tempword;

//printf("%s\n", temp->word); //prints the value (just for debug)

temp->next = hashtable[hash];
hashtable[hash] = temp;
}

for (int i = 0; i < 30; i++)
{
node* temp = (node*)malloc(sizeof(node));
temp = hashtable[i];
while (temp != NULL) //loop to print the linked list
{
if (temp->word != NULL) //**THIS IS WHERE VALGRIND IS POINTING THE ERROR TO BE**
{
printf("%s\n", temp->word);
temp = temp->next;
}
else break;
}
}

printf("\n");
fclose(dict);

printf("SUCCESS!!\n");

}

我哪里出错了?请帮忙

    -----VALGRIND ERROR-----
jharvard@appliance (~/Dropbox/pset5): valgrind ./tempeol
==11035== Memcheck, a memory error detector
==11035== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11035== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==11035== Command: ./tempeol
==11035==
antione
==11035== Conditional jump or move depends on uninitialised value(s)
==11035== at 0x80488CA: main (tempeol.c:68)
==11035==
beer
bear
caterpillar
cat
google
root

SUCCESS!!
==11035==
==11035== HEAP SUMMARY:
==11035== in use at exit: 582 bytes in 74 blocks
==11035== total heap usage: 75 allocs, 1 frees, 934 bytes allocated
==11035==
==11035== LEAK SUMMARY:
==11035== definitely lost: 480 bytes in 60 blocks
==11035== indirectly lost: 102 bytes in 14 blocks
==11035== possibly lost: 0 bytes in 0 blocks
==11035== still reachable: 0 bytes in 0 blocks
==11035== suppressed: 0 bytes in 0 blocks
==11035== Rerun with --leak-check=full to see details of leaked memory
==11035==
==11035== For counts of detected and suppressed errors, rerun with: -v
==11035== Use --track-origins=yes to see where uninitialised values come from
==11035== ERROR SUMMARY: 30 errors from 1 contexts (suppressed: 0 from 0)

Valgrind 显示 30 个错误。我的哈希表大小为 30。所以感觉问题应该出在某个地方。但我不明白为什么?

最佳答案

您对哈希表条目的初始化并未初始化字段word,这就是 valgrind 提示的问题。

此外,您的代码似乎还有其他问题。例如,前一行中的 malloc 立即被覆盖,因此内存泄漏。另外,malloc 的返回值不应被强制转换。尽可能避免施法,将施法视为“施展咒语”。只要您不知道它的作用,就避免它。

关于c - 链表::哈希表::Valgrind错误::条件跳转或移动取决于未初始化的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29188097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com