I have a problem about memory allocated, I read through my code and think threre is no problem, but it did happens, it very confusing me ...., here is my code: the problem lies in a[] when the Class Grid

wants to use it.

```
#include <iostream>
using namespace std;
typedef int Coordinate;
typedef int Threatvalue;
typedef double Wgt;
class GridUnit;
inline ostream& operator <<(ostream& out, GridUnit& p);//
class GridUnit
{
public:
enum NO{N1=8};
GridUnit( ):index(0), x(0),y(0),value(1),a(new Wgt[N1]){
for (int i=0; i<N1; ++i)
{
a[i] = 0.0;
}
cout<<"ctor1 is called!"<<endl;}
~GridUnit( ) {cout<<"dctor is called!"<<endl;
delete[ ] a;}
int getIndex() const {return index;}
Coordinate getX() const{return x;}
Coordinate getY() const{return y;}
Threatvalue getValue() const {return value;}
Wgt getWgt( int i) const {return a[i];}
void setIndex(int newi) {index=newi; } //
void setX(Coordinate newx) {x = newx;}
void setY(Coordinate newy) {y = newy;}
void setValue(Threatvalue newval) {value=newval;}
void setWgt(int i, Wgt v) { a[i]= v;}
void InitialSet(int i, int n){
index=i;
x = index%n;
y = index/n;
value=1;
if (!a)
{
cout<<"//////"<<endl;
}
}
protected:
private:
int index;// 关系 index/dimension=y, index%dim=x;
Coordinate x;
Coordinate y;
Threatvalue value;
Wgt* a;
};
class Grid
{
public:
Grid() { };
Grid(int dim):n(dim),cnt(0),gunit(new GridUnit[dim*dim]) { memset(gunit, 0, n*n*sizeof(GridUnit));}
~Grid( )
{ delete []gunit; }
int getdim() const {return n;}
int getcnt() const {return cnt;}
GridUnit& getGridUnit(int index )
{
if (index>n*n-1)
{cout<<"gunit[index] bound error!"<<'\n';}
return gunit[index];
}
void InitialGrid( ){
int i=0;
int lim=n*n;
for (i=0; i<lim; ++i )
{
GridUnit& tmp = getGridUnit(i);
tmp.InitialSet(i, n);
cnt++;
}//
}
protected:
public:
int n; //dimension
int cnt; //count the number of gridpoints have been initialized
GridUnit* gunit; //array for gridpoints
};
int main()
{
Grid test(2);
test.InitialGrid( ); // this shows that a[] is not exist?
// GridUnit& tmp = test.getGridUnit (0);
// tmp.getWgt (4); // get error?
return 0;
}
```