title: What does "free(): double free detected in tcache 2" mean?
body: <p>I was writing a program for doing polynomial addition in C. I'm having some problems using <code>free()</code>. I have figured out a solution but I don't know what's happening in the background.</p>
  <p>I'm uploading the complete files (instead of code snippet). Here's the <a href="https://github.com/Ankan-Basu/Polynomial-Question-for-StackOverflw" rel="nofollow noreferrer">GitHub repository</a>.  I've included the correct version as <code>polynomial.c</code> and the incorrect version as <code>polynomial_uncorrected.c</code>. The problem is in line 41-42 in <code>polynomial_uncorrected.c</code>.</p>
  <p>I run the program.
  If none of <code>p1</code> and <code>p2</code> are 0 polynomials, then the program is running correctly. If any one of <code>p1</code> or <code>p2</code> is a zero polynomial (all elements are entered as 0), I'm running into the following problem.</p>
  <pre><code>free(): double free detected in tcache 2
  Aborted (core dumped)
  </code></pre>
  <p>Valgrind shows the following output</p>
  <pre><code>==11720== Memcheck, a memory error detector
  ==11720== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==11720== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
  ==11720== Command: ./p_c.out
  ==11720== 
  FIRST POLYNOMIAL :
  Enter degree of polynomial : 2
  Enter coefficient of x^2 : 1
  Enter coefficient of x^1 : 2
  Enter coefficient of x^0 : 3
  
  SECOND POLYNOMIAL :
  Enter degree of polynomial : 0
  Enter coefficient of x^0 : 0
  
  FIRST POLYNOMIAL :
  1x^2 + 2x + 3
  
  SECOND POLYNOMIAL :
  0
  
  SUM :
  1x^2 + 2x + 3
  ==11720== Invalid free() / delete / delete[] / realloc()
  ==11720==    at 0x483A9AB: free (vg_replace_malloc.c:538)
  ==11720==    by 0x1092F2: main (in /&lt;path&gt;/p_c.out)
  ==11720==  Address 0x4a4b960 is 0 bytes inside a block of size 24 free'd
  ==11720==    at 0x483A9AB: free (vg_replace_malloc.c:538)
  ==11720==    by 0x1092CC: main (in /&lt;path&gt;/p_c.out)
  ==11720==  Block was alloc'd at
  ==11720==    at 0x483BD7B: realloc (vg_replace_malloc.c:834)
  ==11720==    by 0x1093D8: inp_pol (in /&lt;path&gt;/p_c.out)
  ==11720==    by 0x10920B: main (in /&lt;path&gt;/p_c.out)
  ==11720== 
  ==11720== 
  ==11720== HEAP SUMMARY:
  ==11720==     in use at exit: 0 bytes in 0 blocks
  ==11720==   total heap usage: 5 allocs, 6 frees, 2,096 bytes allocated
  ==11720== 
  ==11720== All heap blocks were freed -- no leaks are possible
  ==11720== 
  ==11720== For lists of detected and suppressed errors, rerun with: -s
  ==11720== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
  </code></pre>
  <p>I thought p3 was not being allocated so i realloc-ed</p>
  <pre><code>p3 = realloc (p3, (*size3)*sizeof(element));
  </code></pre>
  <p>These lines are still present as comments in the code (polynomial_uncorrected.c). But I got the same error.</p>
  <p>Then I realised I should <code>free(p3)</code> only if both <code>p1</code> and <code>p2</code> are not NULL (ie, not zero polynomial). And it worked, but I don't know why.</p>
  <p><strong>Why was it saying double free detected in tcache 2?</strong></p>