title: I am getting Aborted (core dumped) in below code
body: <p>Hi in my below code for testing <code>realloc()</code> I am getting the error <code>Aborted (Core dumped)</code>. </p>
  
  <p><strong>The output of the code is below</strong></p>
  
  <pre><code>$ ./realloc
  My Name // &lt;&lt;-- This works
  Aborted (core dumped) // &lt;&lt;-- why this error occur?
  </code></pre>
  
  <p>calling to <code>copyThis()</code> function 1st time gives the correct result and produce no error. In the same way if I call <code>copyThis()</code> for the second time, it make error. I can not understand why is it happening. Can any body point me out where the problem is and what the tweak should I do?</p>
  
  <p><strong>The code is below</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  char *copyThis(char *str1, const char *str2, size_t size);
  
  int main(void)
  {
      char *ptr1 = "My Name ";
      char *ptr2 = "is Alice";
      char *ptr3;
      char *ptr4;
  
      ptr3 = copyThis(ptr3, ptr1, strlen(ptr1) + 1); // This works
      printf("%s\n", ptr3);
  
      ptr4 = copyThis(ptr4, ptr2, strlen(ptr2) +1); // this line make Aborted (core dumped)
      printf("%s\n", ptr4);
  }
  
  char *copyThis(char *str1, const char *str2, size_t size)
  {
      str1 = (char *) realloc(str1, size);
      strncat(str1, str2, strlen(str2));
      str1[size] = '\0';
      return str1;
  }
  </code></pre>
  
  <p>NOTE : Please point me any good tutorial site that can help me catch string operations well in c/</p>