1

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

advena
  • 83
  • 13

1 Answers1

1

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.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • great that works pretty well :). But now i wonder is there a possibility to avoid creating array, shuffle it and then save it? – advena Jan 15 '14 at 14:41
  • Fast shuffling: http://stackoverflow.com/questions/196017/unique-random-numbers-in-o1 Article on generating unique random numbers: http://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/ (See the section A Non-Repeating Pseudo-Random Number Generator) – Klas Lindbäck Jan 15 '14 at 14:50
  • Perhaps mention that heap memory not only requires the programmer to think about more (freeing the memory you claim, passing pointers to pointers and the like...) but that _stack_ memory is generally faster, too. Bottom line: really large data-sets: use the heap, if you don't need to: use the stack – Elias Van Ootegem Jan 15 '14 at 15:18
  • thanks I'm still learning C, but have the "feeling" how important is to handle the memory leaks, etc. – advena Jan 15 '14 at 15:23