<wbr><wbr><wbr><span></span>首先,分配一个指针的数组,然后为每一行分配内存。<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"> 函数定义:<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>void **malloc2d(int r, int c, int size)<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>{<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>int i;<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>int **t = malloc(r * sizeof(void*));<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>for(i=0; i<r; i++)<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>t[i] = malloc(c * size);<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>return t;<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><span></span>void free2d(void **arr, int r)<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>{<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>int i = 0;<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>for(i=0; i<r; i++)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>free(arr[i]);<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span></span>free(arr);<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><span></span>这种方法好处是可以用a[i][j]的方式来访问数组元素,但是它需要分配额外内存来存储行指针。</wbr></wbr></wbr>
<wbr><wbr><wbr><span></span>另外,这种方法分配的内存是不连续的,而且在分配和释放二维数组时,要多次调用malloc和free操作,增加了系统开销。而使用以下两种方法为二维数组分配的内存是连续的,可以避免因频繁分配和释放内存而带来的内存开销。</wbr></wbr></wbr>
方法一:
<wbr><wbr><wbr><span></span>用一维数组来实现二维数组的功能,在内存中二维数组还是线性排列的。对于想申请a[m][n]数组可以如下实现:<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>int<wbr><wbr><span></span>*ptr_array;<span style="color:#008000"><br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"></span><wbr><wbr><wbr><span></span>ptr_array<wbr><wbr><span></span>=<wbr><wbr><span></span>(int*) malloc(m*n*sizeof(int));<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"> 当需要访问a[i][j]时,<wbr><wbr><span></span>用如下方式访问:<wbr><wbr><br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><span></span>*(ptr_array<wbr><wbr><span></span>+<wbr><wbr><span></span>i<wbr><wbr><span></span>*<wbr><wbr><span></span>n<wbr><wbr><span></span>+<wbr><wbr><span></span>j);<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"> 当需要释放空间时,用如下方式访问:<br style="line-height:1.5; font-family:宋体,Verdana,Arial,Helvetica,sans-serif"><wbr><wbr><wbr><span></span>free ptr_array;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><span></span>这种方式实际上是把二维数组用一维数组表示,<wbr><span></span>再模拟成二维数组使用.</wbr></wbr></wbr></wbr>
方法二:
void **malloc2d(int row,int col,int size)
{
void **a,*b;
a=(void**)malloc(row*sizeof(void*));
b=(void*)malloc(row*col*size);
if(a!=NULL&&b!=NULL)
while(row--)
a[row]=&b[row*col*size];
return a;
}
void free2d(void **a,void *b)
{
if(a!=NULL&&b!=NULL)
{
free(a);
free(b);
}
}
<wbr><wbr><wbr><span></span>这种方法的好处和不足之处同Robert Sedgewick的方法一样,可以用a[i][j]的方式来访问,但仍然需要额外分配内存来存储行指针。但它分配的空间是连续的,可以避免因频繁分配和释放内存而带来的内存开销,这一点又可与方法一媲美。</wbr></wbr></wbr>
最值得推荐的方法是方法二。