内容的申请和释放
#include <iostream> using namespace std; template <class T> bool Make1DArray(T *x,int num) { x=new int [num]; if (NULL!=x) { return true; } return false; } template <class T> void Delete1DArray(T *x) { if (NULL==x) { return; } delete []x; x=NULL; } template <class T> bool Make2DArray(T **x,int rows,int cols) { x=new T*[rows]; for (int i=0;i<rows;i++) { x[i]=new T[cols]; } return true; } template <class T> void Delete2DArray(T **x,int cols) { if (NULL==x) { return; } for (int i=0;i<cols;i++) { delete[]x[i]; } delete []x; x=NULL; } template <class T> bool Malloc1DArray(T *x,int num) { x=(T *)malloc(sizeof(T)*num); if (x!=NULL) { return true; } return false; } template <class T> void Free1DArray(T *x) { if (NULL==x) { return; } free((void *)x); x=NULL; } template <class T> bool Malloc2DArray(T **x,int nrow,int nclos) { x=(T **)malloc(sizeof(T *)*nrow); for (int i=0;i<nrow;i++) { x[i]=(T *)malloc(sizeof(T)*nclos); } return true; } template <class T> void Free2DArray(T **x,int nrow) { if (NULL==x) { return; } for (int i=0;i<nrow;++i) { free((void *)x[i]); } free((void *)x); x=NULL; } int main() { int n=10; int m=3; cout<<"new一级指针"<<endl; int *p=NULL; Make1DArray<int>(p,10); Delete1DArray<int>(p); cout<<"new二级指针"<<endl; int **q=NULL; Make2DArray<int>(q,m,n); Delete2DArray<int>(q,n); cout<<"malloc一级指针"<<endl; int *r=NULL; Malloc1DArray<int>(r,n); Free1DArray<int>(r); cout<<"malloc二级指针"<<endl; int **t=NULL; Malloc2DArray<int>(t,m,n); Free2DArray<int>(t,m); cout<<"ok"<<endl; }