title: Free : Invalid Size : Valgrind
body: <p>I want to free a malloc by using valgrind :</p>
  
  <p>I receive this error :</p>
  
  <pre><code>MCM : 1900
  free(): invalid size
  Abandon (core dumped)
  </code></pre>
  
  <p>Here is the code I compile :</p>
  
  <pre><code>char test[6][10] = {"MCM", "MCML", "MCMLXXXV", "MCMXC", "MM", "MMVI"};
   int size[] = {3, 4, 8, 5, 2, 4}; 
  
   for (int i = 0; i &lt; 6; i++)
   {   
       char *a = malloc(sizeof(char) * size[i]);
       a = test[i];
       int result = convert(a, roman, arabe, size[i]);
       printf("%s : %d\n",a ,result);
       free(a);
   }   
  </code></pre>
  
  <p>and by using :</p>
  
  <pre><code>valgrind --leak-check=full ./roman
  </code></pre>
  
  <p>I receive this message :</p>
  
  <pre><code>==23809== Memcheck, a memory error detector
  ==23809== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==23809== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  ==23809== Command: ./roman
  ==23809== 
  MCM : 1900
  ==23809== Invalid free() / delete / delete[] / realloc()
  ==23809==    at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==23809==    by 0x108B45: main (roman.c:76)
  ==23809==  Address 0x1fff0003d0 is on thread 1's stack
  ==23809==  in frame #1, created by main (roman.c:47)
  ==23809== 
  MCML : 1950
  MCMLXXXV : 1985
  MCMXC : 1990
  MM : 2000
  MMVI : 2006
  MCM      = 1900
  MCML     = 1950
  MCMLXXXV = 1985
  MCMXC    = 1990
  MM       = 2000
  MMVI     = 2006
  ==23809== 
  ==23809== HEAP SUMMARY:
  ==23809==     in use at exit: 26 bytes in 6 blocks
  ==23809==   total heap usage: 7 allocs, 7 frees, 1,050 bytes allocated
  ==23809== 
  ==23809== 26 bytes in 6 blocks are definitely lost in loss record 1 of 1
  ==23809==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==23809==    by 0x108ABE: main (roman.c:72)
  ==23809== 
  ==23809== LEAK SUMMARY:
  ==23809==    definitely lost: 26 bytes in 6 blocks
  ==23809==    indirectly lost: 0 bytes in 0 blocks
  ==23809==      possibly lost: 0 bytes in 0 blocks
  ==23809==    still reachable: 0 bytes in 0 blocks
  ==23809==         suppressed: 0 bytes in 0 blocks
  ==23809== 
  ==23809== For counts of detected and suppressed errors, rerun with: -v
  ==23809== ERROR SUMMARY: 7 errors from 2 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>From what I see, there are four parts :</p>
  
  <pre><code>==23809== Invalid free() / delete / delete[] / realloc()
  ==23809==    at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==23809==    by 0x108B45: main (roman.c:76)
  ==23809==  Address 0x1fff0003d0 is on thread 1's stack
  ==23809==  in frame #1, created by main (roman.c:47)
  ==23809== 
  </code></pre>
  
  <p>I see that my error is line 76 :</p>
  
  <pre><code>free(a)
  </code></pre>
  
  <p>I don't know how to solve this.</p>
  
  <p>And I don't really understand the HEAP SUMMARY and the LEAK SUMMARY :
  what does it correspond to ?
  HEAP SUMMARY :</p>
  
  <pre><code>==23809== HEAP SUMMARY:
  ==23809==     in use at exit: 26 bytes in 6 blocks 
  ==23809==   total heap usage: 7 allocs, 7 frees, 1,050 bytes allocated
  ==23809== 
  ==23809== 26 bytes in 6 blocks are definitely lost in loss record 1 of 1
  ==23809==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==23809==    by 0x108ABE: main (roman.c:72)
  </code></pre>
  
  <p>LEAK SUMMARY :</p>
  
  <pre><code>==23809== LEAK SUMMARY:
  ==23809==    definitely lost: 26 bytes in 6 blocks
  ==23809==    indirectly lost: 0 bytes in 0 blocks
  ==23809==      possibly lost: 0 bytes in 0 blocks
  ==23809==    still reachable: 0 bytes in 0 blocks
  ==23809==         suppressed: 0 bytes in 0 blocks
  </code></pre>
  
  <p>And the conclusion :</p>
  
  <pre><code>==23809== For counts of detected and suppressed errors, rerun with: -v
  ==23809== ERROR SUMMARY: 7 errors from 2 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>Can someone explain those four parts please ?</p>