百科问答小站 logo
百科问答小站 font logo



写 C C++ 语言时候,如果每次申请内存就把 Free 内存的代码写上,是否能有效避免内存泄漏? 第1页

  

user avatar   warmwine 网友的相关建议: 
      

不能,但我很难解释为什么不能,这问题有点像1+1为什么等于2,显然,但为什么显然是个超难的问题……


user avatar   shengyuli 网友的相关建议: 
      

结论:不能

解决方法:

  • RAII,全称资源获取即初始化(英語:ResourceAcquisitionIsInitialization)。即资源有效期与所管理对象的生命周期严格绑定,通过构造函数完成资源的申请,析构函数完成资源的释放,才能真正意义上避免资源的泄漏。
  • 智能指针(Smart Pointer),通过使用shared_ptr,weak_ptr,unique_ptr三者来实现对指针所指对象的内存自动申请和释放。本质上仍是RAII的一种实现机制。
  • 检测内存泄漏工具

检测内存泄漏:

Valgrind是一款用于内存调试、内存泄漏检测以及性能分析软件开发工具

Example:The Valgrind Quick Start Guide

        #include <stdlib.h>    void f()   {      int* x = malloc(10 * sizeof(int));      x[10] = 0;        // problem 1: heap block overrun   }                    // problem 2: memory leak -- x not freed    int main()   {      f();      return 0;   }     

Memcheck将打印有关其检测到的内存错误和泄漏的消息如下

       ➜  Experiment gcc myprog.c -o myprog ➜  Experiment valgrind --leak-check=yes ./myprog ==1360612== Memcheck, a memory error detector ==1360612== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==1360612== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==1360612== Command: ./myprog ==1360612==  ==1360612== Invalid write of size 4 ==1360612==    at 0x10916B: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==    by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==  Address 0x4b58068 is 0 bytes after a block of size 40 alloc'd ==1360612==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==1360612==    by 0x10915E: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==    by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==  ==1360612==  ==1360612== HEAP SUMMARY: ==1360612==     in use at exit: 40 bytes in 1 blocks ==1360612==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==1360612==  ==1360612== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==1360612==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==1360612==    by 0x10915E: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==    by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612==  ==1360612== LEAK SUMMARY: ==1360612==    definitely lost: 40 bytes in 1 blocks ==1360612==    indirectly lost: 0 bytes in 0 blocks ==1360612==      possibly lost: 0 bytes in 0 blocks ==1360612==    still reachable: 0 bytes in 0 blocks ==1360612==         suppressed: 0 bytes in 0 blocks ==1360612==  ==1360612== For lists of detected and suppressed errors, rerun with: -s ==1360612== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)          




  

相关话题

  C++解析xml有什么好用的轮子? 
  为何C++11与1y都没有将network功能优先加入标准库? 
  为什么看到这么多人不推荐C++? 
  为什么世界上大多数地方的人习惯用逗号表示小数点,而C++却用圆点表示小数点,而不是按照大多数地方习惯? 
  为什么读取连续内存没有比不连续的效率更高? 
  C++ 中的基类为何在析构函数中添加 virtual? 
  C++异常处理写的代码太丑怎么办? 
  p是char类型指针,p[1]不是指向p[0]的下一个字节吗?为什么会到0x11? 
  有没有什么程序库使得我们可以比较方便的在windows下使用比较新版本的opengl的? 
  Linux如何优化程序的体积大小? 

前一个讨论
国家医保局称「新冠病毒疫苗费用 1200 余亿元,由医保基金和财政共同分担」,还有哪些信息值得关注?
下一个讨论
上海 4 月 3 日全市进行抗原检测,4 日全市进行核酸检测,专业角度看将对疫情防控带来哪些帮助?





© 2024-12-25 - tinynew.org. All Rights Reserved.
© 2024-12-25 - tinynew.org. 保留所有权利