#include <stdio.h>
#include <malloc.h>#define OVERFLOW -1
int main(void)
{
int *x = NULL;
int *y = NULL;int* test1();
int* test2();
int* test3();
int* test4();
int* test5();
int* test6();
int* test7();
int* test8();printf("test1: %d/n",*test1());
printf("test2: %d/n",*test2());
x = test3();
printf("test3: %d/n",*x);
if(NULL != x)
free(x);printf("test4: %d/n",*test4());
printf("test5: %d/n",*test5());
printf("test6: %d/n",*test6());
printf("test7: %d/n",*test7());y = test8();
printf("test8: %d/n",*y);
if(NULL != y)
free(y);return 0;
}/*有一个警告,说warning : returning address of local variable or temporary*/
int* test1(void)
{
int a = 1;
return &a;
}int* test2(void)
{
int a = 2;
int *p = &a;
return p;
}int *test3(void)
{
int *a = (int *)malloc(sizeof(int));
if(NULL == a)
{
return NULL;
}
*a = 3;
return a;
}/*也有一个警告,同test1()*/
int *test4(void)
{
int a = *test1(); /*调用test1(),*/
return &a;
}/*由于test1()返回的是局部变量的地址,所以在test4()返回后,
test1()中的局部变量已经被释放,所以test4()返回的指针指向
的内容已经不再存在*/int *test5(void)
{
int a = *test2(); /*调用test2(),*/
return &a;
}int *test6(void)
{
int *p = test2(); /*调用test2(),*/
return p;
}int *test7(void)
{
int p = *test3(); /*调用test3(),返回的是局部变量p的地址,test3中申请有内存将出现泄露*/
return &p;
}int *test8(void)
{
int *p = test3(); /*调用test3(),返回的是指针变量p的地址*/
return p;
}