0

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?

Angelos
  • 169
  • 10
  • 3
    You need to dynamically allocate the array on the heap using `malloc()`. – ruohola Feb 23 '20 at 15:26
  • 1
    The 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
  • 1
    Please post exactly what you did in your question. Also, define "didn't work". – dbush Feb 23 '20 at 15:33

2 Answers2

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
  • 186,650
  • 20
  • 189
  • 240
  • 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
0
#include <stddef.h>

size_t size = 530000;
int arr[size];

would do the work.

By definition, size_t is the unsigned integer type of the result of the sizeof operator. In particular, this is used to return the size of an array or to pass the size you need to dynamically allocate via malloc or similar functions.

alinsoar
  • 14,813
  • 4
  • 53
  • 68
  • 2
    The allocation of 2MB on the stack is not a good idea. – Tarek Dakhran Feb 23 '20 at 15:40
  • @TarekDakhran it's about C, not C++. And I doubt in C++ you cannot do it, or directly via int arr[530000];. Or simply malloc. – alinsoar Feb 23 '20 at 15:42
  • 2
    This is a Variable Length Array; the trouble with very large VLAs is that their allocation may fail when system resources are strained, and when they do fail they fail silently, leaving the door open for undefined behavior. Better to use `malloc()` here, which allows error-checking the allocation and handling of allocation failures. Also, VLAs are only guaranteed to be supported in C99; they were made optional in C11 (and are not supported at all in C++; MSVC did not support VLAs for a long time either, and I don't know if they ever got around to that). – ad absurdum Feb 23 '20 at 16:07