在 C 中动态调整数组大小时出现 Valgrind 错误
Valgrind errors when dynamically resizing array in C
我正在尝试从文件中读取定义为
的值列表
0001Text here
其中0001为id,其余为label。
文件被正确读取并且该部分工作正常但是当我尝试将项目添加到我动态调整大小的数组时 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;
}
我哪里出错了,我该怎么做才能解决这些问题?
C 中的数组索引是从零开始的。您将 nodes
数组的大小调整为 id
的大小,然后访问 nodes[id]
。它不存在,因为它在范围 [0, id-1] 之外。因此,有效元素是 nodes[0]
到 nodes[id-1]
。
我正在尝试从文件中读取定义为
的值列表0001Text here
其中0001为id,其余为label。
文件被正确读取并且该部分工作正常但是当我尝试将项目添加到我动态调整大小的数组时 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;
}
我哪里出错了,我该怎么做才能解决这些问题?
C 中的数组索引是从零开始的。您将 nodes
数组的大小调整为 id
的大小,然后访问 nodes[id]
。它不存在,因为它在范围 [0, id-1] 之外。因此,有效元素是 nodes[0]
到 nodes[id-1]
。