title: C Segmentation fault malloc.c: No such file or directory
body: <p>Here is my code: </p>
  
  <pre><code>// Implements a dictionary's functionality
  
  #include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cs50.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "dictionary.h"
  
  // Maximum length for a word
  #define LENGTH 45
  
  //define struct node
  typedef struct node
  {
      char word[LENGTH + 1];
      struct node *next;
  }
  node;
  
  //hash function
  unsigned int hash(const char word[LENGTH + 1])
  {
      int x = atoi(&amp;word[0]);
      int y;
      if(isupper(word[0]))
      {
          y = (x + 13) % 26;
      }
      if(islower(word[0]))
      {
          y = (x + 7) % 26;
      }
      if(isalpha(word[0]) == 0)
      {
          return 0;
      }
      return y;
  }
  
  //make a hash table of linked lists
  node* hash_table[26];
  
  // Returns true if word is in dictionary else false
  bool check(const char *word)
  {
      node *head = hash_table[hash(word)];
      node *cursor = head;
      while (cursor != NULL)
      {
          if (strcasecmp(word, cursor-&gt;word) == 0)
          {
              return true;
          }
          cursor = cursor-&gt;next;
      }
      return false;
  }
  
  // Loads dictionary into memory, returning true if successful else false
  bool load(const char *dictionary)
  {
      char word[LENGTH + 1];
      FILE *dictionary_file = fopen(dictionary, "r");
      if (dictionary_file == NULL)
      {
          unload ();
          return false;
      }
  
      while (fscanf(dictionary_file, "%s", word) != EOF)
      {
          //allocate memory for node
          node *new_node = malloc(sizeof(node));
          if (new_node == NULL)
          {
              unload();
              return false;
          }
  
          //insert node into linked list
          int index = hash(word);
          node *head = hash_table[hash(word)];
  
          //place word in node
          strcpy(new_node-&gt;word, word);
  
          //connect nodes
          new_node-&gt;next = head;
          hash_table[index] = new_node;
          }
      fclose(dictionary_file);
      return true;
  }
  
  // Returns number of words in dictionary if loaded else 0 if not yet loaded
  unsigned int size(void)
  {
      int sum = 0;
      if (&amp;load)
      {
          char word[LENGTH + 1];
          const char *dictionary;
          FILE *dictionary_file = fopen(dictionary, "r");
          while (fscanf(dictionary_file, "%s", word) != EOF)
          {
              sum++;
          }
      }
      return sum;
  }
  
  // Unloads dictionary from memory, returning true if successful else false
  bool unload(void)
  {
      char word[LENGTH + 1];
      node *head = hash_table[hash(word)];
      node *cursor = head;
  
      while (cursor != NULL)
      {
          node *temp = cursor;
          cursor = cursor-&gt;next;
          free(temp);
      }
  
      if (cursor == NULL)
      {
          return true;
      }
      else
      {
         return false;
      }
  }
  </code></pre>
  
  <p>When I run it through the GDB, I get this message that indicates a segmentation fault: </p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  _int_malloc (av=0x7ffff728d760 &lt;main_arena&gt;, bytes=56) at malloc.c:3777
  3777    malloc.c: No such file or directory.
  </code></pre>
  
  <p>Here is the GDB response when I type "where":</p>
  
  <pre><code>#0  _int_malloc (av=0x7ffff728d760 &lt;main_arena&gt;, bytes=56) at malloc.c:3777
  #1  0x00007ffff6f4dae0 in __GI___libc_malloc (bytes=56) at malloc.c:2893
  #2  0x0000000000422abf in load (dictionary=0x382e332d6e696168 &lt;error: Cannot access memory at address 0x382e332d6e696168&gt;)
      at dictionary.c:66
  #3  0x722d72656c69706d in ?? ()
  #4  0x61732f62696c2f74 in ?? ()
  #5  0x5f72657a6974696e in ?? ()
  #6  0x732f6e6f6d6d6f63 in ?? ()
  #7  0x72657a6974696e61 in ?? ()
  #8  0x632e7367616c665f in ?? ()
  #9  0x6165720000000063 in ?? ()
  #10 0x0063632e31720064 in ?? ()
  #11 0x2828000000000000 in ?? ()
  #12 0x5f76625f706d7421 in ?? ()
  #13 0x287469427465672e in ?? ()
  #14 0x6c00292929786469 in ?? ()
  #15 0x6c6f6f742d6d766c in ?? ()
  </code></pre>
  
  <p>Does anyone have an idea of why the segmentation fault arises? When I run the program through valgrind, the system indicates that no memory has been leaked. Does anyone have an idea of how to fix this?</p>