title: Valgrind error: Conditional jump or move depends on uninitialised value(s) on a char array, even though the array is initialized
body: <p>When I run this code I get the errors: Conditional jump or move depends on uninitialized value(s) and Use of uninitialized value of size 8, both on line 50. I understand that I haven't put any characters/values into &quot;lcword&quot;, but this shouldn't be a problem because I am not assigning the uninitialized values to anything, I'm putting some characters into the array to replace these unitialized values. I don't see any uninitialized values in the condition either - there is only j which is initialized. How should I resolve this problem?</p>
  <pre><code>unsigned int hash(const char *word)
  {
      char* lcword = malloc(46 * sizeof(char));
      for (int j = 0; j &lt; 46; j++)
      {
          lcword[j] = tolower(word[j]); //This is line 50
      }
  
      unsigned int hash = 0;
      for (int i=0, n=strlen(lcword); i&lt;n; i++)
      {
          hash = (hash &lt;&lt; 2) ^ lcword[i];
      }
      free(lcword);
      return hash % N;
  }
  //Source of hash function: https://github.com/andycloke/Harvard-CS50-Solutions/blob/master/pset5/speller/dictionary.c
  </code></pre>
  <p>Here is the whole file (the hash function is being called in the check and the load functions):</p>
  <pre><code>// Implements a dictionary's functionality
  #include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;ctype.h&gt;
  #include &quot;dictionary.h&quot;
  
  // Represents a node in a hash table
  typedef struct node
  {
      char word[LENGTH + 1];
      struct node *next;
  }
  node;
  
  // Number of buckets in hash table
  const unsigned int N = 65536;
  
  // Hash table
  node *table[N];
  unsigned int sizeofdic = 0;
  
  // Returns true if word is in dictionary else false
  bool check(const char *word)
  {
      int i = hash(word);
      node *cursor = table[i];
      while (cursor != NULL)
      {
          if (strcasecmp(cursor-&gt;word, word) == 0)
          {
              return true;
          }
          else
          {
              cursor = cursor-&gt;next;
          }
      }
      return false;
  }
  
  // Hashes word to a number
  unsigned int hash(const char *word)
  {
      char* lcword = malloc(46 * sizeof(char));
      for (int j = 0; j &lt; 46; j++)
      {
          lcword[j] = tolower(word[j]);
      }
  
      unsigned int hash = 0;
      for (int i=0, n=strlen(lcword); i&lt;n; i++)
      {
          hash = (hash &lt;&lt; 2) ^ lcword[i];
      }
          free(lcword);
          return hash % N;
  }
  
  
  // Loads dictionary into memory, returning true if successful else     false
  bool load(const char *dictionary)
  {
      FILE *dict = fopen(dictionary, &quot;r&quot;);
      node *newnode;
      int index;
      char *newword = malloc(46*sizeof(char));
  
      while (fscanf(dict, &quot;%s&quot;, newword) != EOF)
      {
          sizeofdic++;
          newnode = malloc(sizeof(node));
          strcpy(newnode-&gt;word, newword);
          index = hash(newnode-&gt;word);
          newnode-&gt;next = table[index];
          table[index] = newnode;
      }
      fclose(dict);
      free(newword);
      return true;
  }
  
  // Returns number of words in dictionary if loaded else 0 if not yet     loaded
  unsigned int size(void)
  {
  return sizeofdic;
  }
  
  // Unloads dictionary from memory, returning true if successful else     false
  bool unload(void)
  {
      node *cursor;
      node *tmp;
  
      for (int i = 0; i &lt; N; i++)
      {
          cursor = table[i];
          while (cursor != NULL)
          {
              tmp = cursor;
              cursor = cursor-&gt;next;
              free(tmp);
          }
      }
      free(cursor);
      return true;
  }
  </code></pre>