Segmentation error and kernel reset when trying to declare a large array

In my C program, when I try to assign this value to an array:

double sample[200000][2];

I get a segmentation error error. But when I use:

double sample[20000][2]

it works!! Is there a limit to these index values?

+4
source share
2 answers

It looks like you tried to reserve space for 200,000 x 2 = 400,000double values, and each one doublewas 8 bytes, so you tried to reserve about 3.2 megabytes .

, , , Gigs of memory, 1 2 . , 3 , .

, , malloc.
, , -.

malloc:

double (*sample) [200000];
s = malloc(sizeof(*sample) * 2); 
sample[0][0] = 0.0;
sample[1][199999] = 9.9;
+6

, , , .

, visual studio 1MB, . :

SunOS/Solaris   8172K bytes
Linux           8172K bytes
Windows         1024K bytes
cygwin          2048K bytes

, , malloc. C ?, double:

#include <stdlib.h>

double **array1 = malloc(nrows * sizeof(double *));
for(i = 0; i < nrows; i++)
   array1[i] = malloc(ncolumns * sizeof(double));
+6

All Articles