title: Problems with memory dynamic allocation: main: malloc.c:3096: sYSMALLOc
body: <p>I'm writing a simple function to create a list which represents a deck of cards.
  Here's the definition of the structs</p>
  
  <pre><code>typedef struct {
   float valoreEff;
   char *seme;
   char *valore;
   } carta;
  
  struct Mazzo {
   carta info;
   struct Mazzo *nextPtr;
   };
  
  
  typedef struct Mazzo mazzo;
  typedef mazzo *mazzoPtr;
  </code></pre>
  
  <p>Here's the function which returns a pointer to the first element of the list</p>
  
  <pre><code>mazzoPtr caricaMazzo(void){
  
   mazzoPtr sMazzoPtr=NULL;
   int val,seme;
   carta buffer;
   mazzoPtr newPtr;
    char *tabValori[10]={"Asso","Due","Tre","Quattro","Cinque","Sei","Sette","Donna","Cavallo","Re"};
    char *tabSeme[4]={"Denari","Spade","Coppe","Bastoni"};
  
   for(seme=0;seme&lt;4;seme++){
    for(val=0;val&lt;10;val++){
     buffer.seme=tabSeme[seme];
     buffer.valore=tabValori[val];
     if (val&lt;=7) {
      buffer.valoreEff=val+1;
     }
     else {
      buffer.valoreEff=0.5;
     }
     printf("ok\n");
     newPtr=malloc(sizeof(carta));
     if (newPtr==NULL){
      printf("Memoria insufficiente\n");
      return NULL;
     }
     newPtr-&gt;info=buffer;
     newPtr-&gt;nextPtr=sMazzoPtr;
     sMazzoPtr=newPtr;
    }
   }
   return sMazzoPtr;
  }
  </code></pre>
  
  <p>GCC gives me no compile-time errors, but when I execute the program, this is the output</p>
  
  <pre><code>ok
  ok
  main: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
  &amp;((av)-&gt;bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))
  &amp;&amp; old_size == 0) || ((unsigned long) (old_size) &gt;= (unsigned long)
  ((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *
  (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)-&gt;size
  &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed.
  Aborted
  </code></pre>
  
  <p>I've also tried Valgrind, but I can't figure out the mistake in the code.</p>
  
  <pre><code>==21848== Invalid write of size 4
  ==21848==    at 0x8048554: caricaMazzo (in /home/gianluca/Dropbox/PROGRAMMI/progetto/main)
  ==21848==    by 0x8048431: main (in /home/gianluca/Dropbox/PROGRAMMI/progetto/main)
  ==21848==  Address 0x419e034 is 0 bytes after a block of size 12 alloc'd
  ==21848==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
  ==21848==    by 0x804851D: caricaMazzo (in /home/gianluca/Dropbox/PROGRAMMI/progetto/main)
  ==21848==    by 0x8048431: main (in /home/gianluca/Dropbox/PROGRAMMI/progetto/main)
  ==21848== 
  </code></pre>
  
  <p>I hope you can help me :)</p>