If you declare a fixed size array it will be allocated on stack. Stack memory of the program is quite limited. Here are some examples for default values. Also a relevant read: What and where are the stack and heap?
You can either increase stack size... Not recommended but works:
[luk32@localhost tests]$ g++ ./stack_mem.c
[luk32@localhost tests]$ ./a.out
Segmentation fault (core dumped)
[luk32@localhost tests]$ ulimit -s 32768
[luk32@localhost tests]$ ./a.out
[luk32@localhost tests]$ #it worked.
Or dynamically allocate memory on heap:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
srand ( time(NULL) );
int dim = 6;
int n = 1000000;
int size = n * dim;
float *data,r;
data = new float[size];
for(int i = 0; i < size; i++)
{
r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
data[i] = r;
}
delete[] data;
return 0;
}
Result:
[luk32@localhost tests]$ g++ ./stack_mem.c
[luk32@localhost tests]$ ./a.out
[luk32@localhost tests]$
Although, after all I would recommend to use c++ features such as vector
and randoms
.