int create_canvas (int width, int height)
{
if (pData == NULL) {
pData = (char *) memalign (32, width * height);
printf("memalign success pData %p \n", pData);
} else {
char *ptr = (char *)realloc(pData, width * height);
if (ptr == NULL) {
printf("relloc failed and pData %p \n",pData);
pData = NULL;
} else {
pData = ptr;
printf("relloc success pData %p \n", pData);
}
}
}
记录一下 今天学习到的东西 以后作为回顾
以上 借用别人的代码 用ptr 不直接 用pData 是为了防止 返回NULL
返回NULL 第一种情况一个 size =0 也就是width*height =0 这时候需要将pData =NULL 防止崩溃
第二种情况 申请内存失败返回NULL 此时 pData 并没有被释放 也就不能使用 pData = NULL 用了就是内存泄漏了