首页 > 代码库 > C++ delete掉的是new出的堆区内存,无法delete掉栈区的内存

C++ delete掉的是new出的堆区内存,无法delete掉栈区的内存

         直接上代码感受一下:

#include <iostream>
using namespace std;

int main()
{
int a=3;
int* p1=&a;
int* p2=new int(8);
cout<<a<<‘ ‘<<*p1<<‘ ‘<<p1<<‘ ‘<<*p2<<‘ ‘<<p2<<endl;
delete p1;
delete p2;
cout<<a<<‘ ‘<<*p1<<‘ ‘<<p1<<‘ ‘<<*p2<<‘ ‘<<p2<<endl;
return 0;
}

         输出结果:

3 3 0x28fef4 8 0x731870

3 3 0x28fef4 22 0x731870

 

由此可见结论如题所示。

C++ delete掉的是new出的堆区内存,无法delete掉栈区的内存