(edited to correct a mistake and specify code is written for C++)
I am making a large array, P_arr
of size
int N = 30;
float P_arr[27000][4];
with elements given by
float ua_min = 0.0001, ua_max = 0.0075, us_min = 2, us_max = 100, g_min = 0.1, g_max = 0.8;
float ua_start = ua_min + ua_min;
float ua_end = ua_max - ua_max;
float us_start = us_min + us_min;
float us_end = us_max - us_max;
float g_start = g_min + g_min;
float g_end = g_max - g_max;
float ua_stepSize = (ua_end - ua_start) / (N - 1);
float us_stepSize = (us_end - us_start) / (N - 1);
float g_stepSize = (g_end - g_start) / (N - 1);
float ua_arr[3];
float us_arr[3];
float g_arr[3];
ua_arr[0] = ua_start;
us_arr[0] = us_start;
g_arr[0] = g_start;
for (int j = 1; j < N; j++)
{
ua_arr[j] = ua_arr[j - 1] + ua_stepSize;
us_arr[j] = us_arr[j - 1] + us_stepSize;
g_arr[j] = g_arr[j - 1] + g_stepSize;
}
int i = 0;
for (int u = 0; u < N; u++)
{
for (int w = 0; w < N; w++)
{
for (int v = 0; v < N; v++)
{
P_arr[i][2] = g_arr[v];
P_arr[i][1] = us_arr[w];
P_arr[i][0] = ua_arr[u];
i++;
}
}
}
such that the number of rows is always N ^ 3.
Once I increase N > ~ 30, and I increase the size of P_arr
accordingly, the code fails to write the P_arr
and "exits with code: -1073741571."
Is there a length limit to the array I construct? How can I get around this to define larger arrays, say with N = 100?
update
float(*P_arr)[4] = new float[42875][4];
seems to increase the limit of N -> ~ 35 and row limit to N^3 = 42875. Is my dynamic allocation incorrect as it is imposing this limit?
To answer your question, the maximum index is the maximum value for a size_t
type.
The "maximum size" of an array is restricted by your system's memory limitations. This depends on where you put the array.
The stack or local variable area, traditionally has a smaller amount of memory allocated. The idea is that only parameters, small quanities of local variables and return addresses are stored on the stack. This is usually a small set of stuff.
Build systems and the OS grant a program more dynamic memory area, a.k.a. heap. Some designs are that the heap grows towards the stack. Be careful here because an OS may have to swap out your program with another program, and that includes the memory area.
There is another area that doesn't get much discussion, which is the static / automatic / global memory area. For example, a static
variable inside a function gets placed in this area, as well as global variables. This area may be larger than the heap or smaller, depending on the build system and the operating system.
Many operating systems have API to allocate memory and different types of memory. You will need to look up the maximum capacity. One maximum may be the RAM limit of your system. Another would be the size of your hard drive (the OS may use virtual memory and swap to the hard drive).
Some operating systems support allocating memory directly on a file. The operating system will manage the swapping from RAM to file for you. The size limit for this is closer to the available space on your hard drive.
In the ancient days, most computers had very little memory. Data processing and storage used tape drives, since more data could be stored in a file than in system memory. Array processing was performed in smaller blocks or chunks. Temporary files were create as necessary to hold interim results.
Do you really need to hold all that data in memory at once?
Can you break it up into smaller pieces? Smaller pieces are easier for today's processors. Align your data with the size of your processor's data cache or the loading page size. No need to have data around that you are not using.