-1

更新:我已经将静态数组更改为动态数组,但我仍然收到段违规错误,尽管 eclipse 说:

*** glibc detected *** (path to file) double free or corruption (!prev): 0x00000000004093d0 ***

StructHashTable 是一个类型定义...

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

您收到错误是因为您试图修改静态分配的数组“哈希”的左值。每当您定义一个数组时(如在 main 中)

StructHashTable hash[N_ELEMS];

sizeof(StructHashTable)*N_ELEMS字节的内存分配给hash,hash指向第一个字节。这种分配称为静态分配,您不能将哈希指向其他内存分配。它将按照您的指定给出错误,因为无法修改 l 值,即左侧值。你也不能释放分配给哈希的内存。只有当 main 终止时,分配给 hash 的内存才会被释放。

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

如果您希望在运行时调整哈希大小,我建议您在 main 中为哈希使用动态内存分配。

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 回答