title: Valgrind "Memory Error" with complex data type
body: <p>I wrote a C-progam which is a generic implementation of stack with linked-lists. I tried reversing some strings, integer arrays, double arrays, and it worked fine. I used valgrind to check for memory leaks and errors, still no issues as such. However when I try reversing an array of complex data type such as of struct type, I get memory errors even though there are no leaks and the output is correct.</p>
  <p>So,</p>
  <ol>
  <li><p>What I am I doing wrong?</p>
  </li>
  <li><p>What does <code>Invalid write of size 8</code> mean ?</p>
  </li>
  <li><p>Also what does this - <code>Address 0x4e444d8 is 0 bytes after a block of size 8 alloc'd</code> mean ?</p>
  </li>
  </ol>
  <p>// Following is the error log from valgrind:</p>
  <pre><code>==74356== HEAP SUMMARY:
  ==74356==     in use at exit: 0 bytes in 0 blocks
  ==74356==   total heap usage: 7 allocs, 7 frees, 1,096 bytes allocated
  ==74356== 
  ==74356== All heap blocks were freed -- no leaks are possible
  ==74356== 
  ==74356== ERROR SUMMARY: 6 errors from 2 contexts (suppressed: 0 from 0)
  ==74356== 
  ==74356== 3 errors in context 1 of 2:
  ==74356== Invalid read of size 8
  ==74356==    at 0x109425: reverse (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1092DA: main (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==  Address 0x4e44618 is 0 bytes after a block of size 8 alloc'd
  ==74356==    at 0x4A36ECB: malloc (vg_replace_malloc.c:307)
  ==74356==    by 0x109587: newNode (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x109490: push (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1093E3: reverse (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1092DA: main (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356== 
  ==74356== 
  ==74356== 3 errors in context 2 of 2:
  ==74356== Invalid write of size 8
  ==74356==    at 0x4A3E333: memmove (vg_replace_strmem.c:1270)
  ==74356==    by 0x1095A2: newNode (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x109490: push (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1093E3: reverse (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1092DA: main (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==  Address 0x4e444d8 is 0 bytes after a block of size 8 alloc'd
  ==74356==    at 0x4A36ECB: malloc (vg_replace_malloc.c:307)
  ==74356==    by 0x109587: newNode (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x109490: push (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1093E3: reverse (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356==    by 0x1092DA: main (in /home/arnab/MyWorkspace/c-workspace/test/generic_functions/test_node/test)
  ==74356== 
  ==74356== ERROR SUMMARY: 6 errors from 2 contexts (suppressed: 0 from 0)
  
  </code></pre>
  <p>// Following is the program -</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;string.h&gt;
  
  #define SIZE 3
  
  typedef struct node{
      void * data;
      struct node * next;
  }NODE;
  
  typedef struct stack{
      NODE * top;
  }STACK;
  
  
  typedef struct comp{
      double real;
      double imaginary;
  }COMPLEX;
  
  
  NODE * newNode (void * data, size_t size);
  bool isEmpty (NODE * top);
  NODE * push (NODE * top, void * data, size_t size);
  NODE * pop (NODE * top);
  
  void reverse (COMPLEX arr[], int n);     
  
  int main (void)
  {   
      COMPLEX arr[SIZE] = {
                              {1.7, 2.3}, 
                              {2.4, 3.5}, 
                              {3.1, 4.8}
                          };
  
      printf (&quot;Before reversing-\n&quot;);
      
      for (int i = 0; i &lt; SIZE; i++)
          printf (&quot;%.1lf + %.1lfi\n&quot;, arr[i].real, arr[i].imaginary);       
  
      reverse (arr, SIZE);
  
      putchar ('\n');
  
      printf (&quot;After reversing-\n&quot;);
  
      for (int i = 0; i &lt; SIZE; i++)
          printf (&quot;%.1lf + %.1lfi\n&quot;, arr[i].real, arr[i].imaginary);
  
      return 0; 
  }
  
  void reverse (COMPLEX arr[], int n)
  {
      STACK S = {.top = NULL};
  
      for (int i = 0; i &lt; n; i++)
          S.top = push (S.top, &amp;arr[i], sizeof (COMPLEX));
  
  
      for (int i = 0; i &lt; n; i++)
      {
          * ((COMPLEX *) (&amp;arr[i])) = * ((COMPLEX *) (S.top-&gt;data));
          S.top = pop (S.top);
      }
  }
  
  
  NODE * newNode (void * data, size_t size)
  {
      NODE * new_Node = malloc (sizeof (NODE));
      new_Node-&gt;data = malloc (sizeof (size));
      memcpy (new_Node-&gt;data, data, size);
      new_Node-&gt;next = NULL;
      return new_Node; 
  }
  
  bool isEmpty (NODE * top)
  {
      if (top == NULL)
          return true;
      
      return false;
  }
  
  NODE * push (NODE * top, void * data, size_t size)
  {
      NODE * new_Node = newNode (data, size);
  
      if (isEmpty (top))
          top = new_Node;
      else
      {   
          new_Node-&gt;next = top;
          top = new_Node;
      }
      
      return top;
  }
  
  NODE * pop (NODE * top)
  {
      if (isEmpty (top))
          fprintf (stderr, &quot;Stack is empty. Cannot perform pop operation.\n&quot;);
      else
      {
          NODE * next = top-&gt;next;
          free (top-&gt;data);
          free (top);
          top = next;
      }
  
      return top;
  }
  </code></pre>