私はvalgrindからエラー(実際には1トンのエラー)が発生しています。
私はこのコードを使って構造体を宣言しています:
struct HashTableT {
HashFuncT hashFunc;
// array of SortedList's
SortedListPtr* arrayPtr;
};
typedef struct HashTableT* HashTable;
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;
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 of size 4 alloc'd
==18735== at 0x4005B83: malloc (vg_replace_malloc.c:195)
==18735== by 0x804979B: HTCreate (chainedhash.c:32)
==18735== by 0x8049727: main (main.c:457)
35行目はmallocステートメントを持つ行です。それは私が割り振っていると思うので、エラーが私を混乱させるので、私はそれについて何をすべきか理解できません。どんな助けもありがとうございます。
ありがとう…
ベストアンサー
index = malloc(sizeof(HashTable));
あなたの構造体ではなく、ポインタのための十分なメモリです。
これは、このような型を隠すtypedefが、混乱を招く理由を示しています。