# define N 50
Void fun1 (int n)
{
Int * a=new int [n].
for (int i=0; I & lt; n; I++)
{
A [I]=I;
}
The delete [] a;//here unless you use the delete release, otherwise have to wait for the end, because it is impossible for the external release of local variables, this is no problem,
}
Int * fun2 (void)
{
Int a [N]={0};
for (int i=0; I & lt; N; I++)
{
A [I]=I + 5;
}
Return a;//local variables on the stack, the effects on automatically release
}
Int * fun3 (int n)
{
Int * a=new int [n].
for (int i=0; I & lt; n; I++)
{
A [I]=I;
}
Return a;//the heap of local variables as a return value, how to release?
}
Void fun4 (int n, int * a=NULL)
{
;//why don't you do anything, the incoming null pointer also complains?
}
Int main ()
{
Int n=50;
Fun1 (n);
Int * aa=new int [n].//the first kind, the return value and assignment, and then release the external variables,
Aa=fun2 ();
for (int i=0; I & lt; n; I++)
{
STD: : cout & lt;
The delete [] aa;
Int * bb=new int [n].//the first kind, the return value and assignment, and then release the external variables,
Bb=fun3 (n);//function internally created a didn't release?
The delete [] bb;
Int * p=NULL;
Fun4 (n);//an error, why, just passed in parameter is NULL!
Fun4 (n, p);//an error, why, just passed in parameter is NULL!
system("pause");
return 0;
}
CodePudding user response:
Fun2 memory on the stack can't deleteFun3 not delete the memory leak
Produce a chain reaction fun4 error because of wrong before