0

Good afternoon everyone!

I have one problem, new you can see part of load function:

 node* root   = calloc(1, sizeof(node));
 node* cursor = calloc(1, sizeof(node));
 int amount_of_words = 0;
 cursor = root;
 char letter_1 = 'a';

do 
 {
  fread(&letter_1, sizeof(char),1,vocabulary);
  int letter = letter_1;

   if ( (letter >= 'a')&&(letter <= 'z'))
      {
        if (cursor->children[letter - 'a'] == NULL) /*LINE 63*/
         {
          cursor->children[letter - 'a'] = malloc(sizeof(node)); /*LINE 65*/
         }

Valgrind confused a bit, next step is using malloc, but I do not understand why it detected lime 63but not 65 This is the error which Im getting in valgrind:

jharvard@appliance (~/Dropbox/pset5): valgrind ./speller ~cs50/pset6/dictionaries/small text
==12836== Memcheck, a memory error detector
==12836== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==12836== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==12836== Command: ./speller /home/cs50/pset6/dictionaries/small text
==12836== 
==12836== Conditional jump or move depends on uninitialised value(s)
==12836==    at 0x804906A: load (dictionary.c:63)
==12836==    by 0x8048745: main (speller.c:47)
==12836== 

All variables were determined before do-while loop and they were nested with NULL values. In terms of load function ROOT and CURSOR are visible for loop. But i do not understand the reason of error.

During checking in GDB array is filled with NULL values:

65                cursor->children[letter - 'a'] = malloc(sizeof(node));
(gdb) info local
letter = 99
vocabulary = 0x804c008
root = 0x804c170
cursor = 0x804c170
amount_of_words = 0
letter_1 = 99 'c'
(gdb) p cursor.children[1]
$2 = (struct node *) 0x0
(gdb) p cursor.children[1]
$3 = (struct node *) 0x0
(gdb) p cursor.children[2]
$4 = (struct node *) 0x0
(gdb) p cursor.children[26]
$5 = (struct node *) 0x0
(gdb) p cursor.children[2]
$6 = (struct node *) 0x0

Thanks in advance for help!

1

You appear to be adding nodes to your trie using malloc, even though the root node is created with calloc. Where do you initialize the fields of the nodes that you allocate using malloc?

1
  • I haven't noticed it, valgrind confused me a bit. Malloc was used on next step , but not on line which it detected. thanks for help! – LeVl May 17 '15 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .