title: Valgrind giving error: "Conditional jump or move depends on uninitialised value(s)"
body: <p>I'm pretty sure I know what this error means. It's pretty self explanatory. But I don't understand why. Especially because THE ERROR POINTS TO LINES 59 AND 61 ONLY. As in the functions <code>printDictionary</code> and <code>menu</code>. I don't understand why it would work on <code>loadDictionary</code>, which takes the same variables as arguments. Any advice?</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      char **wordArray;   /* declare the dynamic array of strings, not yet allocated.*/
  
      int capacity = INITIAL_ARRAY_MAX;
      int wordCount = 0;
  
      if (argc != 3)
      {
          fprintf(stderr,"Usage: %s inputfile outputfile\n", argv[0]);
          return 1;
      }
  
      if (loadDictionary(argv[1], &amp;wordArray, &amp;wordCount, &amp;capacity) != 0)
      {
          fprintf(stderr,"    loadDictionary failed! Program terminating...\n");
          return 1; /* already in main() so exit unneccesary.*/
      }
  
      printf("\n  Finished loading %d words from dictionary.\n", wordCount);
  
      printDictionary(wordArray, wordCount);   /* Line 59 */
  
      menu(&amp;wordArray, &amp;wordCount, argv[2]);   /* Line 61 */
  
      return 0;
  }
  </code></pre>
  
  <p>Here is my loadDictionary function. It does initialize the array! Also I just tried addign functions before loadDicitonary and any function before it which has the array as an argument works. So the problem clearly lies in loadDictionary.</p>
  
  <pre><code>int loadDictionary(char *inputFileName, char ***array, int *count, int *capacity)
  {
      FILE *inputFile;
      char word[WORD_LENGTH];
  
      if ((inputFile = fopen(inputFileName, "r")) == NULL)
      {
          fprintf(stderr,"Error opening input file, %s\n", inputFileName);
      return -1;
      }
  
      *array = (char **)malloc(*capacity * sizeof(char*));
      if (*array == NULL)
      {
          fprintf(stderr, "Malloc of array in loadDictionary failed!\n");
          return -1;
      }
  
      printf("Reading file %s (each . is 5000 words read)\n", inputFileName);
  
      *count = 0;
      while (fscanf(inputFile, "%s", word) == 1)
      {
          if (*count &gt;= *capacity)
          {
              /*makes new array of double size with contents of original array.*/
              if (doubleArraySize(array, *count, capacity) != 0){
                  fprintf(stderr, "Doubling array failed!\n");
              }
          }
          if (insertWord(*array, count, word) != 0)
          {
              fprintf(stderr,"    Insert returned an error!\n");
              fclose(inputFile);
              return 1;
          }
  
          if (*count % 5000 == 0)
          {
               printf(".");
               fflush(stdout);
          }
      }
  
      fclose(inputFile);
  
      return 0;
  }
  </code></pre>
  
  <p>Valgrind Log.</p>
  
  <pre><code>==6107== Memcheck, a memory error detector
  ==6107== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
  ==6107== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
  ==6107== Command: ./lab2 105-words.txt output.txt
  ==6107== Parent PID: 3994
  ==6107==
  ==6107== Conditional jump or move depends on uninitialised value(s)
  ==6107==    at 0x4089E29: vfprintf (vfprintf.c:1630)
  ==6107==    by 0x4091EFE: printf (printf.c:35)
  ==6107==    by 0x804882F: main (lab2.c:59)
  ==6107==
  ==6107== Conditional jump or move depends on uninitialised value(s)
  ==6107==    at 0x4089E29: vfprintf (vfprintf.c:1630)
  ==6107==    by 0x4091EBE: fprintf (fprintf.c:33)
  ==6107==    by 0x8048F49: menu (lab2.c:271)
  ==6107==    by 0x804884F: main (lab2.c:61)
  ==6107==
  ==6107==
  ==6107== HEAP SUMMARY:
  ==6107==    in use at exit: 0 bytes in 0 blocks
  ==6107==   total heap usage: 110 allocs, 110 frees, 3,197 bytes allocated
  ==6107==
  ==6107== All heap blocks were freed -- no leaks are possible
  ==6107==
  ==6107== For counts of detected and suppressed errors, rerun with: -v
  ==6107== Use --track-origins=yes to see where uninitialised values come from
  ==6107== ERROR SUMMARY: 210 errors from 2 contexts (suppressed: 0 from 0)
  </code></pre>