title: munmap_chunk(): invalid pointer
body: <p>I've spotted the error in my program and decided to write a simple one, which would help me understand what's going on. Here it is :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  char * first()
  {
      char * word = malloc(sizeof(char) * 10);
      word[0] = 'a';
      word[1] = 'b';
      word[2] = '\0';
      return word;
  }
  
  char * second ()
  {
      char * word = malloc(sizeof(char) * 10);
      word = "ab";
      return word;
  }
  
  int main ()
  {
      char * out = first();
      printf("%s", out);
      free(out);
      out = second();
      printf("%s", out);
      free(out);
      return 0;
  }
  </code></pre>
  
  <p>The <code>first()</code> function is working properly, but the <code>second()</code> (exactly the <code>free(out)</code> ) genarates error:</p>
  
  <blockquote>
    <p>Error in `./a.out': munmap_chunk(): invalid pointer: 0x0000000000400714 ***
    ababAborted (core dumped)</p>
  </blockquote>
  
  <p>I don't understand why the first function is correct, but the second isn't. Could anyone explain why?</p>