需要构建一个哈希表,我必须创建一个至少包含 1,000,000 个项目的大型结构体数组。
#include <stdio.h>
#define N 1000003
struct hashTable
{
char productID[16];
double points;
};
int main(int argc, char const *argv[])
{
struct hashTable table[N] = {0}; // Stack Overflow happens here
return 0;
}
问题是每当我尝试创建这样的数组时都会出现堆栈溢出。
有办法克服这个问题吗?还有其他方法可以创建这么大的数组吗?
最佳答案
hashTable *table = malloc(N*sizeof(hashTable)); //allocate memory
... use it
free(table); //free memory
关于无法在 C 中创建大型结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524820/