我正在使用valgrind测试trie,并且在第一个符号传递给函数create_trienode后,“条件跳转或移动取决于未初始化的值”错误。
我有结构:
typedef struct TrieNode{
struct TrieNode **children;
bool is_word;
} TrieNode;
func create_trienode:
struct TrieNode *create_trienode(char c, struct TrieNode *parent){
struct TrieNode *node = malloc(sizeof(struct TrieNode));
node->children = malloc(ALPHABET_SIZE*sizeof(struct TrieNode*));
node->is_word=false;
return node;
}
和func create_tree
struct TrieNode *create_tree(FILE *file)
{
struct TrieNode *root = create_trienode(' ', NULL);
struct TrieNode *ptr = root;
int character;
int converted;
int buffer;
//This handles if file does not end with a newline
character = fgetc(file);
buffer = fgetc(file);
while(character != EOF)
{
character = tolower(character);
if (character == 10) // case newline
{
}
else
if(isalpha(character))
{
converted = character - 'a';
if(ptr->children[converted] == NULL) // CONDITIONAL JUMP HERE
{
ptr->children[converted] = create_trienode(character, ptr);
}
ptr = ptr->children[converted];
}
if (character == 92)
{
if(ptr->children[ALPHABET_SIZE] == NULL)
{
ptr->children[ALPHABET_SIZE] = create_trienode(character, ptr);
}
ptr = ptr->children[ALPHABET_SIZE];
}
if(ptr != root && (!(character == 92|| isalpha(character)) || buffer == EOF))
{
ptr->is_word = true;
ptr = root;
word_count++;
}
character = buffer;
buffer = fgetc(file);
}
{p> 1行上的valgrind说“条件跳转或移动取决于未初始化的值”如何解决这个问题?
答案 0 :(得分:2)
您将ptr->children[converted]
与NULL
进行了比较,但未将其初始化为任何值。
在malloc之后,空格不会为空,因此ptr->children[converted]
不能为“空白”,它可以是任何值。
要修复它,可以在malloc之后将ptr->children[converted]
初始化为NULL。