我正在用valgrind測試trie,並且在第一個符號傳遞給函數create_trienode後發生「條件跳轉或移動取決於未初始化值(s)」錯誤。有條件跳轉問題
我有結構:
typedef struct TrieNode{
struct TrieNode **children;
bool is_word;
} TrieNode;
FUNC create_trienode:上線if(ptr->children[converted] == NULL)
的valgrind
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);
}
說:「條件跳轉或移動依賴於未初始化值(s)「我如何解決這個問題?
首先,初始化應該是'node-> children = malloc(ALPHABET_SIZE * sizeof(struct TrieNode *));'(注意額外的'*'...) –
另外,我不相信這個是你的真實代碼。你已經定義了'create_trienode'來接受一個參數,但是當你調用它時你傳遞了兩個參數。 –
這是一個錯誤create_trienode應該傳遞兩個參數。 Thx用於*注,減去丟失字節的一半 – Tom