title: sYSMALLOc: Assertion failed - How can I solve?
body: <p>I'm writing some simple functions to manage graphs.</p>
  
  <p>When I run my program the following happens error:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I execute valgrind and it shows me this errors:</p>
  
  <pre><code>==5903== Memcheck, a memory error detector
  ==5903== Invalid write of size 4
  ==5903==    at 0x8048866: creategraph
  ==5903==    by 0x8048718: readFile
  ==5903==    by 0x80486BF: main
  ==5903==  Address 0x41c3204 is 0 bytes after a block of size 4 alloc'd
  ==5903==    at 0x4027ABA: malloc (vg_replace_malloc.c:263)
  ==5903==    by 0x8048850: createGraph
  ==5903==    by 0x8048718: readFile
  ==5903==    by 0x80486BF: main
  </code></pre>
  
  <p>This is my struct</p>
  
  <pre><code>typedef struct GRAPH {
     int id;
     int destination;
     int cost;
     struct GRAPH *next;
     struct GRAPH *prev;
  } GRAPH;
  </code></pre>
  
  <p>This is my function readFile</p>
  
  <pre><code>void readFile() {
     FILE *f = NULL;
     char c;
     f = fopen("g.txt", "r");
     if (f == NULL) {
       puts("Error");
     }
     int line = 0, column = 0;
     g = createGraph(16);
     while (!feof(f)) {
       c = fgetc(f);
       if (c == '\n') {
          line++;
       } else if (c == '1') {
          createEdge(line, column, 1, g);
          column++;
       }
     }
    fclose(f);
   }
  </code></pre>
  
  <p>This is my function createGraph</p>
  
  <pre><code>graph **creatgraph(int tV) {
     int i;
     graph **v;
     v = (graph**) malloc(sizeof (graph*));
     if (v == NULL) {
        puts("Error");
        exit(EXIT_FAILURE);
     }
     for (i = 0; i &lt; tV; i++) {
        v[i] = NULL;
     }
     return v;
  }
  </code></pre>
  
  <p>This is my function createVertex</p>
  
  <pre><code>graph *createVertex() {
     graph *newVertex = NULL;
     newVertex = (graph*) malloc(sizeof (graph));
     if (newVertex == NULL) {
        puts("Error");
        exit(EXIT_FAILURE);
     }
     newVertex-&gt;id = 0;
     newVertex-&gt;destination = 0;
     newVertex-&gt;cost = 1;
     newVertex-&gt;next = NULL;
     novoVertice-&gt;prev = NULL;
     return (newVertex);
  }
  </code></pre>
  
  <p>This is my function createEdge</p>
  
  <pre><code>void createEdge(int vStart, int vFinal, int id, graph** g) {
     graph *newVertex = createVertex();
     newVertex-&gt;destination = vFinal;
     newVertex-&gt;id = id;
     g[vFinal] = insertLast(g[vStart], newVertex);
  }
  </code></pre>
  
  <p>Thank you very much for your help.</p>