【问题标题】:sYSMALLOc: Assertion failed - How can I solve?sysmalloc:断言失败 - 我该如何解决?
【发布时间】:2012-04-25 15:57:15
【问题描述】:

我正在编写一些简单的函数来管理图表。

当我运行我的程序时,会发生以下错误:

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.

我执行了 valgrind,它显示了这个错误:

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

这是我的结构

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

这是我的函数 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);
 }

这是我的函数 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;
}

这是我的函数 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);
}

这是我的函数 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);
}

非常感谢您的帮助。

【问题讨论】:

标签: c memory graph malloc


【解决方案1】:

这里有一个内存损坏错误:

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

您只为一个 graph* 指针分配存储空间,但将分配的块视为足够大以容纳 tV 此类指针。

要解决此问题,请将 malloc() 调用更改为:

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

【讨论】:

    【解决方案2】:

    此错误意味着您在某处损坏了内存。例如,使用 Valgrind 运行您的代码,以找出您的程序失败的地方。

    【讨论】:

      【解决方案3】:

      您分配了一个只有一个元素的指针数组。试试:

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

      【讨论】:

        猜你喜欢
        • 2021-02-17
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        相关资源
        最近更新 更多