Ruben Gonçalves
Ruben Gonçalves

Reputation: 61

sYSMALLOc: Assertion failed - How can I solve?

I'm writing some simple functions to manage graphs.

When I run my program the following happens error:

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
&((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))
&& old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)
((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))
+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1)))
&& ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

I execute valgrind and it shows me this errors:

==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

This is my struct

typedef struct GRAPH {
   int id;
   int destination;
   int cost;
   struct GRAPH *next;
   struct GRAPH *prev;
} GRAPH;

This is my function readFile

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);
 }

This is my function createGraph

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 < tV; i++) {
      v[i] = NULL;
   }
   return v;
}

This is my function createVertex

graph *createVertex() {
   graph *newVertex = NULL;
   newVertex = (graph*) malloc(sizeof (graph));
   if (newVertex == NULL) {
      puts("Error");
      exit(EXIT_FAILURE);
   }
   newVertex->id = 0;
   newVertex->destination = 0;
   newVertex->cost = 1;
   newVertex->next = NULL;
   novoVertice->prev = NULL;
   return (newVertex);
}

This is my function createEdge

void createEdge(int vStart, int vFinal, int id, graph** g) {
   graph *newVertex = createVertex();
   newVertex->destination = vFinal;
   newVertex->id = id;
   g[vFinal] = insertLast(g[vStart], newVertex);
}

Thank you very much for your help.

Upvotes: 3

Views: 14024

Answers (3)

wildplasser
wildplasser

Reputation: 44250

You were allocating a pointer array with only one element. Try:

graph **creatgraph(int tV) {
   int i;
   graph **v;
   v = malloc(tV * sizeof *v);
   if (v == NULL) {
      puts("Error");
      exit(EXIT_FAILURE);
   }
   for (i = 0; i < tV; i++) {
      v[i] = NULL;
   }
   return v;
}

Upvotes: 1

md5
md5

Reputation: 23717

This error means that you have corrupted memory somewhere. Run your code with Valgrind for example, to find where your program failed.

Upvotes: 1

NPE
NPE

Reputation: 500733

There is a memory corruption bug right here:

   v = (graph**) malloc(sizeof (graph*));
   ...
   for (i = 0; i < tV; i++) {
      v[i] = NULL;
   }

You're allocating storage for just one graph* pointer, yet treat the allocated block as if it were large enough for tV such pointers.

To fix, change that malloc() call to:

   v = (graph**) malloc(tV * sizeof (graph*));

Upvotes: 5

Related Questions