现在的位置: 首页 > 综合 > 正文

动态分配和释放一、二维数组

2013年10月27日 ⁄ 综合 ⁄ 共 659字 ⁄ 字号 评论关闭

分配:

double **a=new double *[2*numPoints];
 for(int i = 0; i <2*numPoints;i++)
          a[i] = new double[8];
 double *b =new double[2*numPoints];

释放:

for(int i = 0; i<2*numPoints;i++)
 {
  delete [8]a[i];
  a[i]=NULL;
 }
 delete [2*numPoints]a;
 delete []b;
 a=NULL;
 b=NULL;

 如下:

float* fInit1DPointer(int num)
{
 register int i;

 float* p = new float[num];

 for(i=0; i<num; i++)
 {
  p[i]=0.0f;
 }
 return(p);
}

void Free_f1DPointer(float *p)
{
 delete [] p;
}

float** fInit2DPointer(int row,int col)
{
 register int i,j;

 float* arr = new float[row*col];
 float** p = new float*[row];
 for(i=0; i<row; i++)
 {
  p[i] = arr + i*col;

  for(j=0; j<col; j++)
  {
   p[i][j]=(float)0.0;
  }
 }
 return(p);
}

void Free_f2DPointer(float **p)
{
 delete [] *p;
 delete [] p;
}

抱歉!评论已关闭.