Is there a way to make an array of size 530000 in C?

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?

c
arrays
size
asked on Stack Overflow Feb 23, 2020 by Angelos

2 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.

answered on Stack Overflow Feb 23, 2020 by dbush
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.

answered on Stack Overflow Feb 23, 2020 by alinsoar

User contributions licensed under CC BY-SA 3.0