'C++ Help,a memory access error when using memset in a three-dimensional array instantiate with a custom class template
I'm new to C++and I'm learning it now,but I meet a problem when I tried to using memset in an 3D array class I defined. Here's the defination of the class.
template <class T>
class CArray3D
{
T*** array;
int l1;
int l2;
int l3;
public:
CArray3D(int l1,int l2,int l3):l1(l1),l2(l2),l3(l3){
array = new T**[l1];
for(int i = 0;i < l1;++i)
{
array[i] = new T*[l2];
for(int j = 0;j < l2;++j)
{
array[i][j] = new T[l3];
}
}
};
T** operator[](int i){
return array[i];
}
};
and the memset use.
CArray3D<int> a(3,4,5);
// initialize array a with for-loop
memset(a[1],-1 ,20*sizeof(int));
//try to print it with for-loop but met memory access problem
memset(a[1],-1 ,20*sizeof(int));
memset(a[1][1],0 ,5*sizeof(int));
Exception: EXC_BAD_ACCESS (code=1, address=0xffffffffffffffff)
I'm debuging it in mac arm with clang++ lldb.
and I didn't meet the problem when using built-in 3D array a[][][] in same test code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
