title: realloc(): invalid next size - realloc dynamic struct
body: <p>I started learning about struct in C. Today I found a problem, that I can't solve. I have this code: </p>
  
  <pre><code>typedef struct fraze
  {
    char *mostSearch = NULL; // for string from user
    double freq;
  } s_FRAZE;
  
  int readFraze( )
  {
    int i = 2, k;
    size_t len = 0;
    char c;
    s_FRAZE *s;
    s = (s_FRAZE *)malloc( i * sizeof( int ));
    k = 0;
    while( (c = getchar()) != '\n')
    {
      ungetc( c, stdin );
  
      if( scanf( "%lf%c", &amp;s[k].freq, &amp;c) != 2 || c != ':' )
      {
        return 1;
      }
  
      if( k + 1 &gt;= i )
      {
        i *= 2;
        printf("%d\n", i );
        s = (s_FRAZE *)realloc( s, i * sizeof( int ));
      }
  
      len = getline(&amp;s[k].mostSearch, &amp;len, stdin );
      s[k].mostSearch[len-1] = '\0';
      k++;
    }
  
    return 0;
  }
  </code></pre>
  
  <p>I want to read while user don't type '\n', but it works 2x and then I get this erorr  realloc(): invalid next size: 0x0000000001559010 ***
  I tryed use valgrind and there are more errors:</p>
  
  <blockquote>
    <p>==7662== Invalid write of size 8<br>
    ==7662==    at 0x56AEBB4: _IO_vfscanf (vfscanf.c:2328)<br>
    ==7662==    by 0x56BBD3A: scanf (scanf.c:33)<br>
    ==7662==    by 0x40089F: readFraze() (main.c:31)<br>
    ==7662==    by 0x400818: main (main.c:15)<br>
    ==7662==  Address 0x59fe048 is 0 bytes after a block of size 8 alloc'd<br>
    ==7662==    at 0x4C27C0F: malloc (vg_replace_malloc.c:299)<br>
    ==7662==    by 0x400847: readFraze() (main.c:25)<br>
    ==7662==    by 0x400818: main (main.c:15)<br>
    ==7662==<br>
    ==7662== Conditional jump or move depends on uninitialised value(s)<br>
    ==7662==    at 0x56BFCA2: getdelim (iogetdelim.c:63)<br>
    ==7662==    by 0x40093E: readFraze() (main.c:44)<br>
    ==7662==    by 0x400818: main (main.c:15)  </p>
  </blockquote>
  
  <p>Can anyone tell me, what am I doing wrong?</p>