Question

So as the question follows :). I need to save large (about 5,000,000) amount of unique numbers in random order (shuffled) into the file. I've wrote program that works only for ca. 1,000,000. It first creates an array of ordered elements then shuffle them and save each number in new line of the file. But when I'm trying to increase the amount of numbers "Segmentation fault" appears. I think that because I want to allocate so much space in memory for that array at the very beginning.

So is there a better (efficient?) way of saving that numbers? It could be saved directly to the file. I need all that numbers to do some data comparison in C

Was it helpful?

Solution

There are three different ways to allocate memory:

Static allocation:

int data[5000000];    /* allocate outside functions */

or

static int data[5000000];    /* anywhere */

The memory is set up as the program starts.

On the heap:

int *data = malloc(5000000*sizeof int);    

The memory is set up when malloc is called. If there isn't enough free space in the heap, the program will request more memory from the OS.

On the stack:

int data[5000000];    /* allocate inside function */

The memory will be allocated from the stack. If there isn't enough space left on the stack the program will fail.

Static allocation and heap allocation can usually handle large arrays while stack allocation is more limited.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top