I am currently simulating a sample of atoms interacting with each other. In order to do that I am calculating all the relative distances between them in order to calculate the forces. Now, for post analysis I need to have for each time step an array containing all the relative distances. By writing this array in a txt for each time step, I'll get a huge data set. Assume that I have 4 particles. Then the size of the array should be 4*3 in order to avoid storing the distance between one particle with itself. The thing is that I have 729 particles which gives a size of 530712 and I am getting this: "Process returned -1073741571 (0xC00000FD)". I print something exactly after the declaration of this array and it won't get printed unless the size is less than 250000. Any suggestions?
Asked
Active
Viewed 91 times
0
-
3You need to dynamically allocate the array on the heap using `malloc()`. – ruohola Feb 23 '20 at 15:26
-
1The stack size is usually limited to avoid eating up all system memory on error (i.e. an endless recursion). Either allocate your array elsewhere (bss or heap) or increase the process stack size limit (under linux for example with ulimit -s 819200` – Ctx Feb 23 '20 at 15:28
-
I tried that. I declared an array using malloc inside main function and then passed it inside the function where the forces-distances are calculated. It didn't work. But wait, what do you mean by "heap". I am new into programming. – Angelos Feb 23 '20 at 15:29
-
1Please post exactly what you did in your question. Also, define "didn't work". – dbush Feb 23 '20 at 15:33
1 Answers
4
Arrays defined within a function are typically located on the stack. An array of that size will overflow the size of the stack, resulting in the error you're getting.
Instead, use malloc
to dynamically allocate the array. Dynamic allocations can be much larger than what the stack will allow.

dbush
- 205,898
- 23
- 218
- 273
-
I wrote this inside the main function but it didn't work.double* array=malloc(np*(np-1)*sizeof(double)); – Angelos Feb 23 '20 at 15:32