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;
}

I see one problem -- line 61 default constructor does not allocate any memory for GridUnit so when the class is destroyed it will try to deallocate an unallocated pointer. If you don't want the class to allocate memory then set gunit = 0; and all will be well.

I see one problem -- line 61 default constructor does not allocate any memory for GridUnit so when the class is destroyed it will try to deallocate an unallocated pointer. If you don't want the class to allocate memory then set gunit = 0; and all will be well.

That's really a problem I have not noticed. By the way, someone told me that the reason why my code can't run is because the superflous initializiatin in line 61, comment the memset is ok.

After making the change I suggested, I get this output

ctor1 is called!
ctor1 is called!
ctor1 is called!
ctor1 is called!
//////
//////
//////
//////
dctor is called!
dctor is called!
dctor is called!
dctor is called!
Press any key to continue . . .
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.