-1

更新:静的配列を動的に変更しましたが、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...

    }

誰かが私がやろうとしていることをする方法を教えてもらえますか?

ありがとう。

4

1 に答える 1

0

静的に割り当てられた配列「ハッシュ」の左辺値を変更しようとしているため、エラーが発生しています。配列を定義するときはいつでも(メインのように)

StructHashTable hash[N_ELEMS];

sizeof(StructHashTable)*N_ELEMS バイトのメモリがハッシュに割り当てられ、ハッシュは最初のバイトを指します。このような割り当ては静的割り当てと呼ばれ、他のメモリ割り当てへのハッシュ ポイントを作成することはできません。左辺の値、つまり左側の値は変更できないため、指定したとおりにエラーが発生します。ハッシュに割り当てられたメモリを解放することもできません。ハッシュに割り当てられたメモリは、メインが終了したときにのみ解放されます。

// HERE'S THE ERROR...
    hash = newHash;

実行時にハッシュのサイズを変更したい場合は、メインのハッシュに動的メモリ割り当てを使用することをお勧めします。

int main() {
...
//Initial hash table creation
StructHashTable *B0 = (StructHashTable *) malloc(N_ELEMS*sizeof(StructHashTable));
...

...
//Do something
...

...
//hash table full. So resize hash table. This function call need not be inside main. Just for illustration purpose I am doing it here.
B0 = resizeHash(B0);
... 
}

そして、これが変更された resizeHash 関数です。

StructHashTable* resizeHash(StructHashTable *oldHash) {

int size = currentElements + N_ELEMS;
int i; 

//Allocate memory for newHash with larger number of elements.
StructHashTable *newHash = (StructHashTable*) malloc(size * sizeof(StructHashTable));

if (newHash != NULL) {
    for (i = 0; i < currentElements; i++) {
    // Copy one by one oldHash table elements into newhash table
    //Something like below, or whatever you have been doing before to copy.
    newHash[i] = oldHash[i];        
    }
}   

//Free memory occupied by oldHash
free(oldHash);
//Set new value for currentElements 
currentElements = size;

//return the newHash address to calling function.   
return (newHash);   

}
于 2013-08-31T16:19:08.297 に答える