0

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?

enb081
  • 3,831
  • 11
  • 43
  • 66
George Lydakis
  • 399
  • 2
  • 5
  • 23

2 Answers2

1

It's too large to allocate that way (on the stack in most implementations). The standard does not guarantee allocation of objects that large in that fashion. Use malloc() to allocate it dynamically instead.

Randy Howard
  • 2,165
  • 16
  • 26
1
hashTable *table = malloc(N*sizeof(hashTable)); //allocate memory
... use it
free(table); //free memory
brano
  • 2,822
  • 19
  • 15
  • 1
    Casting the result of malloc is pointless and potentially dangerous. Read any [C faq](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Lundin Mar 20 '13 at 13:35
  • thx, fixed it ;) i'm used to c++ – brano Mar 20 '13 at 13:36