Flat 3D array

I have been stuck on this task for quite a while. I need to extend my 2d flat array to 3d. I know that doing it this way isn't the best, but I have to. I include my 2d version and my attempt at 3d. I don't exactly know what I am doing with this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 double ** aloc2df(int eil, int stl){

	double ** mass;
	
	int i, j;
	
	mass = new double*[eil];
	assert(mass != 0);
	mass[0]=new double[eil*stl];
	assert( mass[0] != 0);
	
	
	for(i=1; i<eil; i++){
		mass[i] = mass[0]+i*stl; 				
	}		
	
	for (int k=0; k<eil; k++){
		for (int j=0; j<stl; j++){
		 mass[k][j] = 0;
	}
	}
	
	return mass;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
double *** aloc3df(int eil, int stl, int gyl){

	double *** mass;

	mass = new double**[eil];
	assert(mass != 0);
	mass[0]=new double*[eil*stl];
	assert( mass[0] != 0);
	mass[0][0]=new double[eil*stl*gyl];
	assert( mass[0][0] != 0);
	
	for(int i=1; i<eil; i++){
		mass[i] = mass[0]+ i*stl; 
		
	for(int o=1; o<stl; o++){
		mass[i][o] = mass[i][0] + o*gyl;
	
	for(int p=1; p<gyl; p++){
                // Something should be here ??
	}
	}
	}		
	
	for (int j=0; j<eil; j++){
		for (int k=0; k<stl; k++){
		 	for (int l=0; l<gyl; l++){
		 mass[j][k][l] = 0;
	}
	}
	}
	

	
	return mass;
}
Last edited on
I never liked pointers to pointers for arrays. You could flatten the entire array like this and then only need 1 delete for clean up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(){

	int Height = 10;
	int Width = 10;
	int Depth = 10;

	float* ary = new float[Height*Width*Depth]();

	for (int k = 0; k < Depth; k++)
		for (int r = 0; r < Height; r++)
			for (int c = 0; c < Width; c++)
				/*Init array*/
				ary[k*Height*Width + r*Width + c] = 1;

	delete[] ary;
	return 0;
}
As you can see I am not a fan of it either :d . If I understand correctly you can't use pointer-like notation in the code you showed me ? float[2][1][12] = 5; How am I supposed to access exact array cell ?
Last edited on
You access any cell with the formula on line 13.

Using your example

ary[2][1][12] == ary[12*Height*Width + 2*Width + 1]
I am still looking for help on this one doing it with arrays of pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double *** aloc3df(int eil, int stl, int gyl){

	int i,j,k;
	double ***mass;
	
	mass = new double ** [eil];
	assert (mass != 0);
	for(i=0; i<eil; i++){
		mass[i] = new double * [stl];	
		assert (mass[i] != 0);
	}
	mass[0][0] = new double [eil*stl*gyl];
	assert (mass[0][0] != 0);
	
	for(i=1; i<eil; i++){
	for(j=1; j<stl; j++){
		mass[i][j] = mass[0][0] + i*stl*gyl + j*gyl ;
	}
	}

	return mass;
}
Last edited on
Use your 2D allocator inside the 3D allocator:
1
2
3
4
5
6
7
8
9
10
11
12
13
double *** aloc3df(int eil, int stl, int gyl){

	int i,j,k;
	double ***mass;
	
	mass = new double ** [eil];
	assert (mass != 0);
	for(i=0; i<eil; i++){
		mass[i] = alloc2d(stl, gvl);
		assert (mass[i] != 0);
	}
	return mass;
}
Topic archived. No new replies allowed.