我正在阅读一个将指针初始化为空的代码,最初我认为代码没有为指针分配新内存以存储二维值数组(它确实如此),这让我想知道,是一个初始化为空指针的指针分配内存?
class Int8_tSet : public GridSet
{
public:
int8_t** set;
//
//
Int8_tSet():set(0) {}
Int8_tSet( const Int8_tSet& x ):set(0) {copy(x);}
virtual ~Int8_tSet() { Free2DArray(set);}
//
// --- opeartor
int8_t operator() ( IntVector2D x ) const {return set[x.i][x.j];}
int8_t& operator() ( IntVector2D x ) {return set[x.i][x.j];}
// --- function
void Set();
void Set(int8_t val);
void Set( IntVector2D x ){ NS_GRIDDATA::Set(x,*this,(int8_t)-1); }
void Set( IntVector2D x,int8_t val){ NS_GRIDDATA::Set(x,*this,val); }
void Switch();
// --- output & input
void Output(std::ostream& out ) const;
void Input (std::istream& in );
// --- copy & substitute
public:
void copy( const Int8_tSet& x ) {NS_GRIDDATA::Copy(*this,x,(int8_t) -1);}
const Int8_tSet& operator = ( const Int8_tSet& x );
void Extract(IntVector2D &ll, IntVector2D &ur,Int8_tSet &subgrid) const;
};
void Int8_tSet::Set()
{
if(!set) {std::cerr<<" Memory not allocated. Call Set(x) first \n"; exit(1); }
std::fill(set[0],set[0]+size.i*size.j,-1);
}
我意识到它在void Set中分配内存。