C++第01课 C++与C的区别(五)

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;
}

 

上一篇:删除win10无限嵌套文件夹


下一篇:Mac的光标后删除内容