百科问答小站 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++ 早就过时了,大部分写工程不用 C++,学习这个语言只是为了竞赛」的观点是否正确? 
  怎样吸引优秀的C++程序员? 
  C语言 乘以0.01快?还是除以100快? 
  c#多播或event监听太多后gc和时间都会爆炸,那么比起List<Action>存在的意义是什么? 
  如果编译器遇到未定义行为把硬盘格式化了,需要负法律责任吗? 
  c++中超出char、int范围的整数怎么存储? 
  C++原子变量数组能用memset置空吗? 
  编译器是如何实现C/C++中的const的功能? 
  为什么 C++ std::map::operator[] 不提供 const 版本? 
  C++ 是否适合做 GUI? 

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





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