title: Can't free() value allocated by malloc() in another function
body: <p>I have a C function, something like this:</p>
  
  <pre><code>void foo(char ** out) {
       *out = malloc(computedsize);
       if(*out != NULL){
           sprintf(*out, "%s,%s", foovar, baa);
        }
  }
  </code></pre>
  
  <p>and then I call: </p>
  
  <pre><code>int main(void) {
     char * out = NULL;
     foo(&amp;out);
     printf("%s\n", out); /* so far, it works fine */
     free(out); /* the problem. */ 
  }
  </code></pre>
  
  <p>When I call:</p>
  
  <p><code>free(out);</code></p>
  
  <p>it given:</p>
  
  <pre><code>*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x09a03050 ***
  ======= Backtrace: =========
  /lib/tls/i686/cmov/libc.so.6(+0x6b161)[0x4ff161]
  /lib/tls/i686/cmov/libc.so.6(+0x6c9b8)[0x5009b8]
  /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x503a9d]
  ./a.out[0x804875b]
  ./a.out[0x804871a]
  ./a.out[0x80486f9]
  /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x4aabd6]
  ./a.out[0x8048601]
  ======= Memory map: ========
  </code></pre>
  
  <p>//do need copy memory map part?</p>
  
  <p>Can someone point out my mistake? I believe that's <code>sprintf()</code> call.. or no, actually, I have no idea. I tried too make a new variable inside function alloc value for it, and then <code>*out = myvariable;</code> and <code>*out = strdup(myvariable)</code> but the <code>free()</code> call give same error.</p>
  
  <h2>Update</h2>
  
  <p>I see that the problem is inside the function. Any <code>free()</code> call inside it,causes the invalid next size.</p>
  
  <p>for example:</p>
  
  <pre><code>char *f=malloc(2);
  strcpy(f,"a");
  free(f);
  </code></pre>
  
  <p>Inside <code>foo()</code> function, gets the above error, on <code>main()</code> function, works normally. I'm completely lost how to fix this.</p>