dynamic_malloc

dynamic allocate two dimention array:

int (*a)[10]=(int(*)[10])malloc(sizeof(int)*10*10);

推薦另一種動態配置的寫法,
用這種方法配出來的記憶體區塊是連續的,
釋放記憶體時簡單,也可以用 a[x][y] 這種型式的定址法。

double** mat = (double**)malloc( h*sizeof(double* )+
                                    w*h*sizeof(double));
  double* x = (double* )(mat + h);
  for(int y = 0; y!=h ; ++y, x+=w )
    mat[y] = x;

  free(mat);
c
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License