Answer's Question about pointer

When you create a new pointer, this will be in heap until you delete it. 
So what you said is sort of mistake, "函数内部有个局部变量指针", then the pointer should not exist after the function return.

Therefore, you should delete the pointer before the program is done, or memory leak

int* add(){
int a =1;
int *Ptr=&a;
return Ptr; //for this Ptr is not actual local variable, because it be return, so other can use it
}

int add(){
int a =1;
int *Ptr=&a;
delete Ptr;
return 0; //for this Ptr is local variable, because it was deleted
}
posted @ 2017-05-15 10:12  zhchoutai  阅读(145)  评论(0编辑  收藏  举报