• 2022-06-25 C++老八股之 new 的内存用 free 回收有危险


    C++老八股之 new 的内存用 free 回收有危险


    关于 new delete malloc free

    C++为什么要用 new 和 delete 。

    大概是为了解决某些含有资源,需要确保通过析构,释放资源的类的对象留存。你可以认为是为了保障 RAII 的一种设置。

    C则不必为此操心,虽然为此会更操心,但是操心也没卵用,没有class封装,只能整天提心内存泄漏,更没有 RAII 如此优雅的机制。


    举个例子

    对于内置类型,是 OK 的,对只含有内置类型的类对象,也是 OK 的。

    因为其内存和类实体是合一的,实体即内存。

    以下代码是不安全的,只供学习!

    #include <iostream>
    #include <vector>
    
    struct canFree
    {
        int intArry[3] = {0, 1, 2};
    };
    
    int main()
    {
        canFree *testCanFree = new canFree();
    
        int *rawDataCf = testCanFree->intArry;
    
        std::cout << "testCanFree before free " << std::endl;
    
        for (int i = 0; i != 3; ++i)
        {
            std::cout << *(rawDataCf + i) << std::endl;
        }
    
        free(testCanFree);
    
        std::cout << "testCanFree after free " << std::endl;
    
        for (int i = 0; i != 3; ++i)
        {
            std::cout << *(rawDataCf + i) << std::endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    对于非内置类型,包括标准容器,则必然是 LeakMem。

    因为类实体和内存是分离的,实体只包含一个符号,符号指向内存,把符号抹除,内存还在。

    #include <iostream>
    #include <vector>
    
    struct canNotFree
    {
        std::vector<std::string> strVec = {"free", "and", "memleak"};
    };
    
    int main()
    {
        canNotFree *testCanNotFree = new canNotFree();
    
        std::string *rawDataCnf = testCanNotFree->strVec.data();
    
        std::cout << "testCanNotFree before free " << std::endl;
    
        for (int i = 0; i != 3; ++i)
        {
            std::cout << *(rawDataCnf + i) << std::endl;
        }
    
        free(testCanNotFree);
    
        std::cout << "testCanNotFree after free " << std::endl;
    
        for (int i = 0; i != 3; ++i)
        {
            std::cout << *(rawDataCnf + i) << std::endl;
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    总结

    相信如果运行过上面的代码,就比较容易理解了。

    深入理解,可能还是需要自己实现一个符号和资源分离的类,比如 vector 容器,还要理解符号和实体之间的关系。

    我写下月亮两个字,你能知道这是天上的阴晴圆缺,我把月亮两个字从笔记上擦掉,你晚上抬头,依旧悲欢离合,没有任何区别。

  • 相关阅读:
    2-1、IOC思想以及实现
    线上mysql表字段加不了Fail to get MDL on replica during DDL synchronize,排查记录
    MinIO与MySQL对比以及存储的相关知识
    Jsp-九大内置对象
    【Spring注解必知必会】深度解析@Configuration注解
    ctfhub技能树---http协议
    ArduinoUNO实战-第十一章-4位共阴数码管实验
    cpp primer plus笔记012-STL模板库
    一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
    [MRCTF2020]Ezpop(详解)
  • 原文地址:https://blog.csdn.net/m0_54206076/article/details/125463265