Search code examples
carraysstructstack-overflow

Cannot create large struct array in C


In need of constructing a hash table, I have to create a large struct array with at least 1,000,000 items.

#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;
}

The problem is that I get a stack overflow whenever I try to create such an array.

Is there a way to overcome this? Is there another way to create such large array?


Solution

  • hashTable *table = malloc(N*sizeof(hashTable)); //allocate memory
    ... use it
    free(table); //free memory