title: signal 11 (SIGSEGV)
body: <p>I wrote the following code:</p>
  
  <pre><code>FILE *book;
  wchar_t bufferln[FILE_READ_BUFFER];
  wchar_t buffer[FILE_READ_BUFFER];
  book = fopen(file, "r");
  if(book == NULL){
      perror("Es ist ein Fehler beim lesen des Buches aufgetreten");
      return EXIT_FAILURE;
  }
  while(fgetws(buffer, FILE_READ_BUFFER, book) != NULL){
      if(wcscmp(buffer, L"\n") == 0){
          bufferln[0] = L'\0';
          continue;
      }
      buffer[wcsnlen(buffer, FILE_READ_BUFFER)-1] = L' ';
      wcsncat(bufferln, buffer, FILE_READ_BUFFER);
  }
  return EXIT_SUCCESS;
  </code></pre>
  
  <p>It crashes with SIGSEGV. I ran valgrind which shows the following:</p>
  
  <pre><code>==11251== Conditional jump or move depends on uninitialised value(s)
  ==11251==    at 0x40BD5CF: wcsncat (wcsncat.c:36)
  ==11251==    by 0x804865D: read_book (book.c:18)
  ==11251==    by 0x804872B: main (main.c:19)
  ==11251==  Uninitialised value was created by a stack allocation
  ==11251==    at 0x80485B7: read_book (book.c:3)
  ==11251== 
  ==11251== Invalid read of size 4
  ==11251==    at 0x40A58E2: fgetws (iofgetws.c:52)
  ==11251==    by 0x804867D: read_book (book.c:12)
  ==11251==    by 0x6D: ???
  ==11251==  Address 0x65 is not stack'd, malloc'd or (recently) free'd
  ==11251== 
  ==11251== 
  ==11251== Process terminating with default action of signal 11 (SIGSEGV)
  ==11251==  Access not within mapped region at address 0x65
  ==11251==    at 0x40A58E2: fgetws (iofgetws.c:52)
  ==11251==    by 0x804867D: read_book (book.c:12)
  ==11251==    by 0x6D: ???
  ==11251==  If you believe this happened as a result of a stack
  ==11251==  overflow in your program's main thread (unlikely but
  ==11251==  possible), you can try to increase the size of the
  ==11251==  main thread stack using the --main-stacksize= flag.
  ==11251==  The main thread stack size used in this run was 8388608.
  </code></pre>
  
  <p>I think the problem is somehow related to my usage of wcsncat (writing into *book memory maybe?) but why?
  I want to read a document (UTF-8) paragraph by paragraph and then do stuff that is not in this code yet.</p>