【发布时间】:2010-10-27 17:07:58
【问题描述】:
Valgrind 在以下代码中报告错误Invalid read of size 8。
我有一个声明的数组,
struct symbol *st[PARSER_HASH_SIZE];
当我的程序初始化时,这个数组中的所有元素都初始化为0。
memset(&st[0], 0, sizeof(st));
我的程序创建struct symbol 的实例并根据哈希值插入到上述数组中。因此,该数组中的元素很少是 NULL,而其他元素将是有效值。
以下代码尝试删除分配的项目,并且 valgrind 在该行抱怨,
sym = st[i]; sym != NULL; sym = sym->next
struct symbol *sym = NULL;
/* cleaning the symbol table entries */
for(i = 0; i < PARSER_HASH_SIZE; i++) {
for(sym = st[i]; sym != NULL; sym = sym->next) { /* <-- Valgrind complains here */
free(sym);
}
}
我正在尝试了解此错误的原因。
任何帮助都会很棒!
【问题讨论】: