c - Conditional jump problems -


i'm testing trie valgrind, , having "conditional jump or move depends on uninitialised value(s)" error after first symbol pass function create_trienode.

i have struct:

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

and 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 not end 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); } 

on line if(ptr->children[converted] == null) valgrind says "conditional jump or move depends on uninitialised value(s)" how solve problem?

you compares ptr->children[converted] null, don't initialized value.

after malloc, space isn't nulled, ptr->children[converted] mustn't 'null', value.

to fix it, init ptr->children[converted] null after malloc.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -