更新:静的配列を動的に変更しましたが、Eclipse は次のように言っていますが、まだセグメント違反エラーが発生します。
*** glibc detected *** (path to file) double free or corruption (!prev): 0x00000000004093d0 ***
StructHashTable は typedef です...
int main() {
...
StructHashTable *B0 = (StructHashTable *) malloc(N_ELEMS*sizeof(StructHashTable));
...
}
void resizeHash(StructHashTable *hash) {
int size = currentElements + N_ELEMS;
StructHashTable newHash[size];
int i;
for (i = 0; i < size; i++) newHash[i].key = FREE;
for (i = 0; i < currentElements; i++) insertHash(newHash, hash[i]);
currentElements = size;
hash = (StructHashTable *) realloc(hash, size*sizeof(StructHashTable));
if (hash != NULL) {
for (i = 0; i < size; i++) hash[i] = newHash[i];
}
}
今何が問題なのですか?悪い方法で realloc を使用していますか? または何?Cは私を夢中にさせています...
OLD: 私は大学の宿題に取り組んでいて、C で静的配列のサイズを変更する必要があります。
配列を宣言するメイン関数があります...
// File: main.c
int main() {
...
StructHashTable hash[N_ELEMS];
...
}
ランタイムのある時点で、N_ELEMS よりも多くの要素が必要になり、HashTable.c でそれを行う関数を作成しました。これがメソッドです。
// File: HashTable.c
#define N_ELEMS 32
int currentElements = N_ELEMS
void resizeHashTable(StructHashTable *hash) {
int size = currentElements + N_ELEMS;
StructHashTable newHash[size];
int i;
// Inicialize newHash
for (i = 0; i < size; i++) newHash[i].key = FREE;
// Insert old hash elements to the new table...
for (i = 0; i < currentElements; i++) {
insertHash(newHash, hash[i]);
}
currentElements = size;
// I've tried making hash null with no luck...
//hash = NULL;
//free(hash);
// HERE'S THE ERROR...
hash = newHash;
// I've tried *hash = *newHash with the same result...
}
誰かが私がやろうとしていることをする方法を教えてもらえますか?
ありがとう。