【问题标题】:Valgrind errors when dynamically resizing array in C在 C 中动态调整数组大小时出现 Valgrind 错误
【发布时间】:2018-02-14 13:06:40
【问题描述】:

我正在尝试从文件中读取定义为的值列表

0001Text here

其中 0001 是 id,其余的是标签。

文件被正确读入并且该部分工作正常,但是当我尝试将项目添加到我动态调整大小的数组时,Valgrind 会出现以下错误:

==9005== Invalid read of size 8
==9005==    at 0x108DB0: processFile (in /trees)
==9005==    by 0x108BEE: main (in /trees)
==9005==  Address 0x521d368 is 0 bytes after a block of size 24 alloc'd
==9005==    at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9005==    by 0x10903E: growArray (in /trees)
==9005==    by 0x108D76: processFile (in /trees)
==9005==    by 0x108BEE: main (in /trees)
==9005== 
==9005== Invalid write of size 8
==9005==    at 0x108DD6: processFile (in /trees)
==9005==    by 0x108BEE: main (in /trees)
==9005==  Address 0x521d368 is 0 bytes after a block of size 24 alloc'd
==9005==    at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9005==    by 0x10903E: growArray (in /trees)
==9005==    by 0x108D76: processFile (in /trees)
==9005==    by 0x108BEE: main (in /trees)
==9005== 

我意识到错误出现在我在下面的代码中标记的行上,但我无法弄清楚为什么会发生这些错误。我认为这可能与未正确初始化的值有关,但我不确定这是正确的。

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>

typedef struct node {
          char  *label;
  unsigned int   n;
} NODE;
typedef NODE TREE;

int growArray(TREE **, int, int);
int processFile(FILE **, char *);

int main (int argc, char **argv) {
  FILE *fp;   /* the file pointer */

  processFile(&fp, argv[1]);
  return 0;
}

int processFile(char *filename) {
  if ((*fp = fopen(fileName, "r")) == NULL) {
      printf("Unable to read file: %d: %s\n", errno, strerror(errno));
      exit(1);
  }

  /* array to hold all nodes. The index is the nodeID */
  int SIZE = 1;
  TREE *nodes = (TREE *)calloc(SIZE, sizeof(NODE));

  if (nodes == NULL) {
    fprintf(stderr, "Cannot allocate initial memory for array.\n");
    exit(1);
  }

  /* checks the line is 4 digits, followed by 63 characters that aren't a */
  /* carriage return or newline                                           */
  int id; char text[64];
  while (fscanf(*fp, " %4d%63[^\r^\n] ", &id, text) == 2){
    SIZE = growArray(&nodes, SIZE, id);

    /* ----------error line---------- */
    nodes[id].label = (char *)realloc(nodes[id].label, (strlen(text)+1));

    strcpy(nodes[id].label, text);

    fprintf(stderr, "%5d: %s\n", id, text);
    return 0;
  }
}

int growArray(TREE **array, int curSize, int id) {
  if (curSize > id) return curSize;

  TREE *temp = (TREE *)realloc(*array, (id * sizeof(NODE)));

  if (temp == NULL) {
    fprintf(stderr, "Cannot allocate more memory.\n");
    exit(1);
  } else {
    *array = temp;
  }
  return id;
}

我在哪里出错了,我可以做些什么来解决这些问题?

【问题讨论】:

  • 代码不足。请发minimal reproducible example,强调Minimal。
  • 对realloc() 的一般警告...如果新的内存块分配失败,realloc() 返回NULL。如果您像以前那样将返回的指针分配给旧的指针,而 realloc() 失败,则您丢失了指向仍然分配的旧内存的指针(然后您不能 free())。
  • Err.... 您只能在从先前对 *alloc() 的调用返回的指针上调用 realloc()。我没有看到你在任何地方分配nodes[id].label,你只是realloc()它......?
  • 是否为成员分配了内存:struct node **children; ?
  • @ryyker 我实际上并不需要那行代码,这是我之前制作的程序留下的

标签: c arrays pointers tree valgrind


【解决方案1】:

C 中的数组索引是从零开始的。您将nodes 数组的大小调整为id,然后访问nodes[id]。它不存在,因为它超出范围 [0, id-1]。因此,有效元素是nodes[0] 到nodes[id-1]。

【讨论】:

  • 我打电话给growArray(&amp;nodes, SIZE, id) 到growArray(&amp;nodes, SIZE, id+1),它解决了原来的问题。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2018-01-26
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多