Memory leak is when dynamic memory is allocated and used, because the pointer to the address is not deleted, the system can not recycle this memory for reuse.
For example, the following program uses“new” to get dynamic memory, uses it up, and finally deletes the pointer to dynamic memory with “delete”, so there will be no memory leak.
In addition, it can be found that dynamic memory allocation is often associated with pointers (addresses). It has great flexibility, but its efficiency and security are lower than static address allocation. Static address allocation is carried out at compile time or before program execution.
#include<iostream>
using namespace std;
int main()
{
int *pi=new int(0);
int *pa=new int[10];
while(*pi<10)
pa[*pi]=(*pi)++;
for(int i=0;i<10;i++)
cout<<pa[i]<<' ';
cout<<endl;
delete pi;
delete pa;
}