美文网首页
2021-10-20

2021-10-20

作者: voidnil | 来源:发表于2021-10-20 11:47 被阅读0次

    macO下C++ delete堆指针并不会真的删除指针的指向地址和改地址中保存的值,仅标记为空闲的内存。

    void heapDemo() {
        
        int *pointer = pointerfunc();
        
        cout << pointer << endl;
        cout << pointer << endl;
        cout << pointer << endl;
        delete pointer;
        int *p2 = new int(11);
        cout << "p2 = " << p2 << endl;
        delete p2;
        cout << "delete p2 = " << p2 << endl;
    }
    
    int * pointerfunc() {
        //在堆区创建整形数据
        //new返回的是该数据类型的指针
        int * pointer = new int(11);
        return pointer;
    }
    

    输出

    0x10524b300
    0x10524b300
    0x10524b300
    p2 = 0x10524b300
    delete p2 = 0x10524b300
    

    相关文章

      网友评论

          本文标题:2021-10-20

          本文链接:https://www.haomeiwen.com/subject/gortaltx.html