百科问答小站 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)          




  

相关话题

  深入理解nginx中,第9章中有一句:利用指针的最后一位一定是0的特性。能解释一下这个特性? 
  倒序输出字符串c++为啥不行? 
  是 C++ 的发展进入了邪路,还是我写代码的姿势不正确? 
  怎样理解C++中“如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题”? 
  有没有必要让 12 岁孩子学 C++? 
  为什么IT书籍的翻译质量相比其他尤其被诟病? 
  C/C++有什么库可以完成命令行参数解析? 
  有没有必要让 12 岁孩子学 C++? 
  C++20有哪些让你激动不已的新特性? 
  为什么当年 Android 选择用 Java 作为开发语言? 

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





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