1.new和delete
new用来申请内存,delete用来释放内存
#include <iostream> using namespace std; int main() { int* p = new int; delete p; p = nullptr; int* pp = new int[4]; delete []pp; pp = nullptr; //申请内存并初始化 int* pData = new int(100); cout << *pData << endl; delete pData; pData = nullptr; int* ppData = new int[4]{ 1,2,3,4 }; for (int i = 0; i < 4; i++) { cout << *(ppData + i) << endl; } delete[]ppData; ppData = nullptr; return 0; }