int func(int n)
{
int i = 0;
int ret = 0;
int *p = (int *)malloc(sizeof(int) * n);
do
{
if(NULL == p) break;
if(n < 0) break;
for (i = 0; i < n; ++i)
{
p[i] = i;
printf("%d\n",p[i]);
}
ret = 1;
}while(0);
free(p);
return ret;
}
do……while
假如修改如下:
int func(int n)
{
int i = 0;
//int ret = 0;
int *p = (int *)malloc(sizeof(int) * n);
if(NULL == p) return 0;
if(n < 0) return 0;
for (i = 0; i < n; ++i)
{
p[i] = i;
printf("%d\n",p[i]);
}
// ret = 1;
free(p);
return 1;
}
假如在检测参数n时候,返回,导致内存泄露,没有free(p)