c++ new 开辟内存堆区 delete 关键字释放堆区数据
#include <iostream> using namespace std; //1.new的基本用法 //new 开辟内存堆区 int * func(){ int *p = new int(10); return p; } void test(){ int *p = func(); //堆区数据由程序员开辟,也由程序员释放 cout << *p << endl; cout << *p << endl; cout << *p << endl; //delete 关键字释放堆区数据 delete p; cout << *p << endl; //非法操作,程序报错 } int main() { test(); return 0; }