以c语言为例,相应的代码实现可以展示如下
#include<stdio.h>
#include<assert.h>
typedef int em_type;
em_type **new_mat(int rows,int cols)
{
em_type **rt=NULL;
int i;
assert(rt=malloc(sizeof(em_type *)*rows)!=NULL);
for(i=0;i<rows;i++)
{
assert((rt[i]=malloc(sizeof(em_type)*cols))!=NULL);
}
return rt;
}
void delete_mat(em_type **s,int rows)
{
int i;
for(i=0;i<rows;i++)
{
//有两次malloc,就一定有两次free
free(s[i);
}
free(s);
}