【问题标题】:Resize static array in C在C中调整静态数组的大小
【发布时间】:2013-08-31 14:55:01
【问题描述】:

更新: 我已将静态数组更改为动态数组,但仍然收到段冲突错误,尽管 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 快把我逼疯了……

旧: 我正在做大学作业,我需要在 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...

    }

有人能告诉我该怎么做吗?

谢谢。

【问题讨论】:

  • 无法调整静态分配数组的大小。你的作业可能不会这么说。
  • 可能是设计问题?因为作业说得很清楚......
  • 如果您的作业是用英语写的,请考虑在您的答案或评论中发布该部分。 无法调整分配的大小。 您唯一能做的就是复制新分配的内存中已有的内容并释放旧的分配;但这意味着您需要动态分配。
  • 作业准确地说是:记录使用依赖于键的哈希表存储,表的初始大小为 32 个元素,填充时相应扩展。
  • 所以,它没有说任何关于静态数组的内容。只有你必须选择初始数组大小,这可以很容易地用动态的来完成。将哈希表实现为静态数组会限制您增加其初始大小。解决方案是将静态数组更改为动态数组。

标签: c arrays static resize


【解决方案1】:

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

StructHashTable hash[N_ELEMS];

sizeof(StructHashTable)*N_ELEMS字节的内存分配给hash,hash指向第一个字节。这种分配称为静态分配,您不能将哈希指向其他内存分配。它会按照您的指定给出错误,因为无法修改左值,即左侧值。你也不能释放分配给哈希的内存。只有当 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);   

}

【讨论】:

  • 好的,我已经完成了,但仍然出现错误,我已经用新代码更新了帖子......无论如何,谢谢。
  • 您对 free 和 realloc 的使用引起了问题。请参阅我上面发布的代码以获得更好的理解。
猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 2020-11-28
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多