3d dynamic array

Hi everybody
I want to use a 3d dynamic array my code is :
1
2
3
4
5
6
7
8
9
10
11
int h=1000000;
int k=5;

long double ***c;
c= new long double**[h];
for(int i=0;i<h; i++){
 c[i]= new long double*[h];
for(int j=0;j<h; j++)
c[i][j]= new long double[k];
}


but it does work properly.
output is:

segment fault



any help is appreciated
Last edited on
The code is fine, but you try to allocate something like 36Tb and something tells me you don't have this much RAM.
You array is (1000000 * sizeof(long double**)) + (1000000 * sizeof(long double *)) + (5 * sizeof(long double)) bytes large. Don't think you have the memory for that. What is it, 20 tB?
Last edited on
my RAM is 4G.
I changed it to
h=1024
but it still doesn't work.

1
2
3
4
5
6
7
8
9
10
11
int h=1024;
int k=5;

long double ***c;
c= new long double**[h];
for(int i=0;i<h; i++){
 c[i]= new long double*[h];
for(int j=0;j<h; j++)
c[i][j]= new long double[k];
}

On the last level you're creating over a million small arrays. Since successive calls to malloc() doesn't allocate contiguous memory blocks, it's possible (although IMO unlikely) that this overhead is causing problems.
Does new long double[h*h*k] fail as well?
but it still doesn't work.

Works for me. What is the problem in your case? Still some segmentation fault?

could you please tell me how should it be used
what is before and what is next
1
2
3
?
new long double[h*h*k]
?
Topic archived. No new replies allowed.