title: realloc() invalid old size
body: <p>I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it.</p>
  
  <p>Here is what I have written (partially, some part is taken from the book directly):-</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int MAXLINE =  10;
  int INCREMENT = 10;
  
  void copy(char longest[], char line[]){
      int i=0;
  
      while((longest[i] = line[i]) != '\0'){
          ++i;
      }
  }
  
  int _getline(char s[]){
      int i,c;
  
      for(i=0; ((c=getchar())!=EOF &amp;&amp; c!='\n'); i++){
          if(i == MAXLINE - 1){
              s = (char*)realloc(s,MAXLINE + INCREMENT);
  
              if(s == NULL){
                  printf("%s","Unable to allocate memory");
                  //  goto ADDNULL;
                  exit(1);
              }
  
              MAXLINE = MAXLINE + INCREMENT;
          }
          s[i] = c;
      }
  
      if(c == '\n'){
          s[i] = c;
          ++i;
      }
  
  ADDNULL:
      s[i]= '\0';
      return i;
  } 
  
  int main(){
      int max=0, len;
  
      char line[MAXLINE], longest[MAXLINE];
  
      while((len = _getline(line)) &gt; 0){
          printf("%d", MAXLINE);
          if(len &gt; max){
              max = len;
              copy(longest, line);
          }
      }
  
      if(max&gt;0){
          printf("%s",longest);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The moment I input more than 10 characters in a line, the program crashes and displays:-</p>
  
  <pre><code>*** Error in `./a.out': realloc(): invalid old size: 0x00007fff26502ed0 ***
  ======= Backtrace: =========
  /lib64/libc.so.6[0x3d6a07bbe7]
  /lib64/libc.so.6[0x3d6a07f177]
  /lib64/libc.so.6(realloc+0xd2)[0x3d6a0805a2]
  ./a.out[0x400697]
  ./a.out[0x40083c]
  /lib64/libc.so.6(__libc_start_main+0xf5)[0x3d6a021b45]
  ./a.out[0x400549]
  </code></pre>
  
  <p>I also checked <a href="https://stackoverflow.com/questions/11406311/realloc-invalid-old-size">realloc invalid old size</a> but could not follow the logic of passing a pointer to a pointer to the function modifying the array.</p>