title: Valgrind errors with dynamic arrays of struct in other strust
body: <p>I have struct like:</p>
  
  <pre><code>typedef struct Sentense{
      long int length;
      wchar_t *str;
      wchar_t **splword;
      long int count_words;
      long long int prod;
  } sentense_s;
  
  typedef struct Text{
      long int quant;
      sentense_s *sent;
  } text_s;
  </code></pre>
  
  <p>And functions for reading text and working with it:</p>
  
  <pre><code>#ifndef MAKE_BIG
  #define MAKE_BIG 10
  #endif
  
  int read_str(wchar_t **str){
      int avl_sy = MAKE_BIG;
      int symb = 0;
      *str = (wchar_t *)malloc(avl_sy * sizeof(wchar_t));
      if (!str)
          return 1;
      wchar_t input = '.';
      while (input != L'\n'){
          wscanf(L"%lc", &amp;input);
          if (avl_sy &lt;= symb){
              avl_sy += MAKE_BIG;
              *str = (wchar_t*)realloc(*str, avl_sy * sizeof(wchar_t));
              if (!str)
                  return 1;
          }
          (*str)[symb] = input;
          symb++;
      }
      (*str)[symb] = L'\0';
      return 0;
  }
  
  int make_arr(wchar_t *temp, text_s *text){
      temp[wcslen(temp) - 1] = L'\0';
      text-&gt;quant = 0;
      long int avl_sent = MAKE_BIG;
      text-&gt;sent = (sentense_s *)malloc(avl_sent * sizeof(sentense_s));
      if (!text-&gt;sent)
          return 1;
      wchar_t *token = NULL;
      wchar_t *buffer = NULL;
      token = wcstok(temp, L".", &amp;buffer);
      while (token != NULL){
          if (avl_sent &lt;= text-&gt;quant){
              avl_sent += MAKE_BIG;
              text-&gt;sent = (sentense_s *)realloc(text-&gt;sent, avl_sent * sizeof(sentense_s));
              if (!text-&gt;sent)
                  return 1;
          }
          text-&gt;sent[text-&gt;quant].str = (wchar_t *)malloc( (wcslen(token) + 2) * sizeof(wchar_t));
          if (!text-&gt;sent[text-&gt;quant].str)
              return 1;
          wmemcpy(text-&gt;sent[text-&gt;quant].str, token, wcslen(token) + 1);
          text-&gt;quant++;
          token = wcstok(NULL, L".", &amp;buffer);
      }
      token = NULL;
      buffer = NULL;
      free(temp);
      return 0;
  }
  </code></pre>
  
  <p>And there are two problems:
  1) In some test it crashes at the begging of program with:</p>
  
  <pre><code>main: malloc.c:2385: sysmalloc: Assertion `(old_top == initial_top (av) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) &gt;= MINSIZE &amp;&amp; prev_inuse (old_top) &amp;&amp; ((unsigned long) old_end &amp; (pagesize - 1)) == 0)' failed.
  </code></pre>
  
  <p>But then I start is second time it works.
  2) Valgrind show me this errors:</p>
  
  <pre><code>==11408== Invalid write of size 4
  ==11408==    at 0x109462: read_str (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x109296: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==  Address 0x4a86878 is 0 bytes after a block of size 680 alloc'd
  ==11408==    at 0x4839D7B: realloc (vg_replace_malloc.c:826)
  ==11408==    by 0x109410: read_str (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x109296: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408== 
  ==11408== Invalid read of size 4
  ==11408==    at 0x483FE24: wcslen (vg_replace_strmem.c:1868)
  ==11408==    by 0x1094AE: make_arr (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x1092C1: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==  Address 0x4a86878 is 0 bytes after a block of size 680 alloc'd
  ==11408==    at 0x4839D7B: realloc (vg_replace_malloc.c:826)
  ==11408==    by 0x109410: read_str (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x109296: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408== 
  ==11408== Invalid free() / delete / delete[] / realloc()
  ==11408==    at 0x48389AB: free (vg_replace_malloc.c:530)
  ==11408==    by 0x1092E5: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==  Address 0x4a865d0 is 0 bytes inside a block of size 680 free'd
  ==11408==    at 0x48389AB: free (vg_replace_malloc.c:530)
  ==11408==    by 0x1096B7: make_arr (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x1092C1: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==  Block was alloc'd at
  ==11408==    at 0x4839D7B: realloc (vg_replace_malloc.c:826)
  ==11408==    by 0x109410: read_str (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408==    by 0x109296: main (in /home/nikita/GitHub/Course-work/Code/main)
  ==11408== 
  </code></pre>
  
  <p>Where is my mistakes? Why it works strange with dynamic array of structs?</p>
  
  <p>It's temp from main:</p>
  
  <pre><code>wchar_t *temp;
  </code></pre>