title: C Segmentation fault even when using EOF
body: <p>I'm loading a file into memory and I am doing so with the following statement:</p>
  
  <pre><code>        if ((ch = fread(&amp;temp[i],1,1,loadDict)) == EOF)
              break;
  </code></pre>
  
  <p>But I receive a segmentation fault. Upon inspection using gdb I verified that the fault is happening at this line (the if statement, before the break). Why does it not see that it will fault (the whole point of using EOF)? </p>
  
  <p>I thought it might be that I'm using the EOF in an if statement rather than within a while() statement. Is it possible to use EOF in an if statement?</p>
  
  <p>Update: More Code</p>
  
  <pre><code>bool load(const char* dictionary)
  {
  FILE* loadDict = fopen(dictionary, "r");
  
  char* new = malloc(sizeof(char)); // for storing the "new-line" character
  *new = 0x0A;
  
  // defines a node, which containes one word and points to the next word
  typedef struct node
  {
  char* word;
  struct node* next;
  }
  node;
  
  
      node* head = malloc(sizeof(node));
      node* temp = malloc(sizeof(node));
      head-&gt;next=temp;
  
      // copies a word from the dictionary file into a node
  
      int* numStore = malloc(sizeof(int)); //count for number of words in dictionary
      int num = 0;
      int ch = 0; // to hold for EOF
      int flag = 0; // for breaking out of while loop
      while(true)
      {
      node* newNode = malloc(sizeof(node));
      temp-&gt;next=newNode;
      temp-&gt;word=malloc(46);
  
          int i = -1;
  
          do
          {
          i++;
          if (!feof(loadDict) || !ferror(loadDict))
          {
              flag = 1;
              break;
          }
          fread(&amp;temp[i],1,1,loadDict);
  
          if  (memcmp (new, &amp;temp[i], 1) == 0)
              num += 1;
          }
          while(memcmp (new, &amp;temp[i], 1) != 0);
  
      temp=newNode;
  
      if (flag == 1)
          break;
      }
  
     numStore = &amp;num;
  
  return true;
  }
  </code></pre>