小编job*_*929的帖子

Valgrind无效的写错误与指向结构的指针数组

我从valgrind那里得到一个错误(好吧,实际上有很多错误),我整理出来时遇到了麻烦.

我正在使用此代码声明一个结构:

struct HashTableT {

HashFuncT hashFunc;
// array of SortedList's
SortedListPtr* arrayPtr;
};

typedef struct HashTableT* HashTable;
Run Code Online (Sandbox Code Playgroud)

arrayPtr是指向其他结构的指针数组的指针.然后用这个为它分配内存:

HashTable index;
index = malloc(sizeof(HashTable));
memcheck(index);
index->hashFunc = func;
index->arrayPtr = malloc(sizeof(SortedListPtr) * size);
memcheck(index->arrayPtr);
// initialize array
int i;
for (i = 0; i < size; i++) {
    index->arrayPtr[i] = NULL;
}
return index;
Run Code Online (Sandbox Code Playgroud)

Valgrind给了我这个错误:

==18735== Invalid write of size 4
==18735==    at 0x80497F1: HTCreate (chainedhash.c:35)
==18735==    by 0x8049727: main (main.c:457)
==18735==  Address 0x402e02c is 0 bytes after a block …
Run Code Online (Sandbox Code Playgroud)

c valgrind

1
推荐指数
1
解决办法
1155
查看次数

标签 统计

c ×1

valgrind ×1