malloc分配数组(1)
int **array;
array = (int *)malloc((row)*sizeof(int));
for(i=0;i<row;i++)
{
array[i]=(int *)malloc((column)*sizeof(int));
}
分配数组(2)
int **array,*elements;
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
array=Malloc(int *, row);
elements=Malloc(int, row*column);
j=0;
for(i=0;i<row;i++)
{
array[i]=&elements[j];
j+=column;
}
(3)用最少malloc调用次数在堆空间上分配二维数组问题
int** myDAlloc(int rows, int columns)
{
int nHead = rows*sizeof(int*);
int nElem = rows*columns*sizeof(int);
int** pArray = (int**)malloc(nHead+nElem);
int* p = (int*)(pArray+rows);
for(int k = 0; k < rows; ++k)
{
pArray[k] = p + k*columns;
}
return pArray;
}
int **array;
array = (int *)malloc((row)*sizeof(int));
for(i=0;i<row;i++)
{
array[i]=(int *)malloc((column)*sizeof(int));
}
分配数组(2)
int **array,*elements;
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
array=Malloc(int *, row);
elements=Malloc(int, row*column);
j=0;
for(i=0;i<row;i++)
{
array[i]=&elements[j];
j+=column;
}
(3)用最少malloc调用次数在堆空间上分配二维数组问题
int** myDAlloc(int rows, int columns)
{
int nHead = rows*sizeof(int*);
int nElem = rows*columns*sizeof(int);
int** pArray = (int**)malloc(nHead+nElem);
int* p = (int*)(pArray+rows);
for(int k = 0; k < rows; ++k)
{
pArray[k] = p + k*columns;
}
return pArray;
}