百科问答小站 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++1y又给C++增加了多少坑? 
  为什么大多数的 C++ 的开源库都喜欢自己实现 string? 
  C++中,auto关键字有哪些乱用的情况?平时使用有哪些坑? 
  #define 不是简单的替换吗,为什么下面的代码错误? 
  C++ 允许「我们都是人,所以我可以把你私有的眼睛借来随便玩,再还给你」,这难道是一种设计上的妥协? 
  有哪些关于C++高性能服务器开发的高质量博客? 
  C 语言线程间怎么通信? 
  GCC 4 编译警告:warning: dereferencing type-punned pointer will break strict-aliasing rules 有什么比较好的解决办法? 
  面向对象中接口应该更抽象还是更具象? 
  C 语言是否需要改进,甚至重新设计一门语言取代 C 语言? 

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





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