The program should not work. But the reason is another than you think. C++ does not allow to declare arrays with non-const expressions as the array size. So here
int p[i];
the compiler shall issue an error.
But it seems it is an extension of C++ provided by the compiler. So the code is compiled successfully.
As for the "memory management" then it is the compiler that resposible for the memory management because the array is defined in the stack. Your participation is not required.
Also I would like to append that this code (the if statement)
1 2 3 4 5 6 7
if(p==NULL)
{
cout<<"No memory";
}
else
{
// ...
has no any sense. If the code was successfully compiled then the address of the first element of an array defined by the compiler in the stack is always unequal to zero. Otherwise the code would not be compiled.