how to delete a 3-d array?

I create a class template to help me build a 3-d array.
but I got "heap corruption detected after normal block #165 c++"
during the class destructor when I try to deallocate the memory.

thanks for any advice.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  #include <iostream>
using namespace std;




template <class T>
	class CArray3D {

	private:

		T*** p3DArray;

		int a_;
		int b_;
		int c_;

	public:

		CArray3D(int a, int b, int c):a_(a),b_(b),c_(b) {

			p3DArray = new T**[a_];

			for (int i = 0; i < a_; i++) {

				
				p3DArray[i] = new T*[b_];

				for (int j = 0; j < b_; j++) {


						p3DArray[i][j] = new T[c_];


				}


			}



		}



		T** operator[](int a) {

			return p3DArray[a];
		}




		~CArray3D() {

			for (int i = 0; i < a_; i++) {

				for (int j = 0; j < b_; j++) {

					delete[] p3DArray[i][j];
					
				}
				
				delete[] p3DArray[i];
			}

			delete[] p3DArray;
		}



	};




int main()
{
	CArray3D<int> a(3, 4, 5);
	int No = 0;
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 4; ++j)
			for (int k = 0; k < 5; ++k)
				a[i][j][k] = No++;
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 4; ++j)
			for (int k = 0; k < 5; ++k)
				cout << a[i][j][k] << ",";
	return 0;
}
Suggestion #1: Don't use an [array of pointers to arrays of pointers to arrays] to begin with. Just use a single array.
1
2
3
4
5
6
7
8
int width = /*...*/;
int height = /*...*/;
int depth = /*...*/;
int *matrix = new int[width * height * depth];

int &cell_xyz = matrix[x + y * width + z * width * height];

delete[] matrix;
Last edited on
Hi,

wut you said make sense to me, but I need to use the form like this : a[i][j][k] to access the data.

If I created a 1-d array, it would be difficult to overload three [][][] to achieve the same function?

If I use the pointer, then I can just overload the first [] and return a pointer that can access the data I want.
I would suggest to not even bother with this and just use an at(size_t, size_t, size_t) member.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
template <typename T, size_t N>
class ArrayAccessor{
    T *array;
    size_t *dimensions;
    size_t offset;
    ArrayAccessor<T, N - 1> next;
public:
    ArrayAccessor(T *array, size_t offset, size_t *dimensions):
            array(array),
            offset(offset),
            dimensions(dimensions){
        
    }
    ArrayAccessor<T, N - 1> &operator[](size_t offset){
        this->next = ArrayAccessor<T, N - 1>(
            this->array + this->offset * *this->dimensions,
            offset,
            this->dimensions + 1
        );
        return this->next;
    }
};

template <typename T>
class ArrayAccessor<T, 1>{
    T *array;
    size_t offset;
public:
    ArrayAccessor(T *array, size_t offset, size_t *dimensions):
        array(array),
        offset(offset)
        {}
    T &operator[](size_t offset){
        return this->array[this->offset * *this->dimensions];
    }
};

template <typename T>
class Matrix3{
    T *array;
    size_t width, height, depth;
    size_t dimensions[3];
public:
    Matrix3(size_t width, size_t height, size_t depth){
        //...
        this->dimensions[0] = 1;
        this->dimensions[1] = width;
        this->dimensions[2] = width * height;
        //...
    }
    ArrayAccessor<T, 3> operator[](size_t offset){
        return ArrayAccessor<T, 3>(this->array, offset, this->dimensions);
    }
};
Last edited on
Topic archived. No new replies allowed.