无法在 C 中创建大型结构数组

标签 c arrays struct stack-overflow

需要构建一个哈希表,我必须创建一个至少包含 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/

相关文章:

c - 为什么在 C 中展平多维数组是非法的?

c# - 在结构上隐藏无参数构造函数

c - 不确定我理解这一行 : "struct x {void (*y)(struct x *z);}"

c - 不是一切都在它应该在的地方,为什么会出现段错误?

c - strcmp 的奇怪行为 [C]

python - Numpy 数组减法 : inconsistent values for large arrays

c++ - 为什么 OpenCV 中的 DMatch 是结构体而不是类

c - 如何通过C编程打开终端中显示的链接?

c - 宏内#if#endif

c - 使用 goto 跳转到内部或兄弟范围