C++智能指针——auto_ptr详解

【C++】auto_ptr为何被唾弃?以及其他智能指针的学习 1. C++ 98中产生了第一个智能指针auto_ptr.给出了更实用的scoped_ptr和shared_ptr和weak_ptr.3. C++ TR1,引入了shared_ptr等。不过注意的是TR1并不是标准版。4. C++ 11,引入了unique_ptr和shared_ptr和weak_ptr。需要注意的是unique_ptr对应boost的scoped_ptr。并且这些智能指针的实现原理是参考boost中的实现的。 阅读详情

前言
从之前智能指针的介绍中可以了解到智能指针主要作用是管理内存,避免内存泄漏和悬垂指针。
之前介绍的不管是智能指针与引用计数详解还是智能指针与句柄详解,其核心就是通过一个类来管理被new出来的对象,具体的技术就是靠引用计数。

auto_ptr介绍
auto_ptr的实现原理其实就是RAII,在构造的时候获取资源,在析构的时候释放资源,并进行相关指针操作的重载,使用起来就像普通的指针。该智能指针已经在C++11中被弃用了,已经有更好的unique_ptr来代替。
官网说明:
template class auto_ptr;
Automatic Pointer [deprecated]
Note: This class template is deprecated as of C++11. unique_ptr is a new facility with a similar functionality, but with improved security (no fake copy assignments), added features (deleters) and support for arrays. See unique_ptr for additional information.

auto_ptr特点

1. auto_ptr没有使用引用计数,在复制构造函数和赋值构造函数中将对象所有权转移了。

template<typename _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
template<typename _Tp1>auto_ptr& operator=(auto_ptr<_Tp1>& __a) throw()
{
    reset(__a.release());
    return *this;
}

void reset(element_type* __p = 0) throw()
{
    if (__p != _M_ptr)
    {
    delete _M_ptr;
    _M_ptr = __p;
    }
}

element_type* release() throw()
{
    element_type* __tmp = _M_ptr;
    _M_ptr = 0;
    return __tmp;
}

因此使用该智能指针的时候要注意:

std::auto_ptr pa(new ClassA());
motherTest(pa); //该函数会导致pa的copy构造函数执行,所有权被转移
pa->…; //调用ClassA对象的方法会crash

2. auto_ptr不能指向数组,因为auto_ptr在析构的时候只是调用delete,而数组应该要调用delete[]。
auto_ptr只有一个私有成员变量_M_ptr

 template<typename _Tp> class auto_ptr
{
private:
    _Tp* _M_ptr;
// 其它代码省略
}

3.auto_ptr不能和标准容器(vector,list,map…)一起使用。
STL容器中的元素经常要支持拷贝,赋值等操作,在这过程中auto_ptr会传递所有权。

下一章:shared_ptr共享指针详解

扩展:刚才想上传免费资源,但是默认要花费积分,所以直接粘贴出来了,auto_ptr源码参考:

// auto_ptr implementation -*- C++ -*-

// Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file backward/auto_ptr.h
 *  This is an internal header file, included by other library headers.
 *  Do not attempt to use it directly. @headername{memory}
 */

#ifndef _BACKWARD_AUTO_PTR_H
#define _BACKWARD_AUTO_PTR_H 1

#include <bits/c++config.h>
#include <debug/debug.h>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   *  A wrapper class to provide auto_ptr with reference semantics.
   *  For example, an auto_ptr can be assigned (or constructed from)
   *  the result of a function which returns an auto_ptr by value.
   *
   *  All the auto_ptr_ref stuff should happen behind the scenes.
   */
  template<typename _Tp1>
    struct auto_ptr_ref
    {
      _Tp1* _M_ptr;
      
      explicit
      auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
    } _GLIBCXX_DEPRECATED;


  /**
   *  @brief  A simple smart pointer providing strict ownership semantics.
   *
   *  The Standard says:
   *  <pre>
   *  An @c auto_ptr owns the object it holds a pointer to.  Copying
   *  an @c auto_ptr copies the pointer and transfers ownership to the
   *  destination.  If more than one @c auto_ptr owns the same object
   *  at the same time the behavior of the program is undefined.
   *
   *  The uses of @c auto_ptr include providing temporary
   *  exception-safety for dynamically allocated memory, passing
   *  ownership of dynamically allocated memory to a function, and
   *  returning dynamically allocated memory from a function.  @c
   *  auto_ptr does not meet the CopyConstructible and Assignable
   *  requirements for Standard Library <a
   *  href="tables.html#65">container</a> elements and thus
   *  instantiating a Standard Library container with an @c auto_ptr
   *  results in undefined behavior.
   *  </pre>
   *  Quoted from [20.4.5]/3.
   *
   *  Good examples of what can and cannot be done with auto_ptr can
   *  be found in the libstdc++ testsuite.
   *
   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
   *  127.  auto_ptr<> conversion issues
   *  These resolutions have all been incorporated.
   */
  template<typename _Tp>
    class auto_ptr
    {
    private:
      _Tp* _M_ptr;
      
    public:
      /// The pointed-to type.
      typedef _Tp element_type;
      
      /**
       *  @brief  An %auto_ptr is usually constructed from a raw pointer.
       *  @param  p  A pointer (defaults to NULL).
       *
       *  This object now @e owns the object pointed to by @a p.
       */
      explicit
      auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }

      /**
       *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
       *  @param  a  Another %auto_ptr of the same type.
       *
       *  This object now @e owns the object previously owned by @a a,
       *  which has given up ownership.
       */
      auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }

      /**
       *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
       *  @param  a  Another %auto_ptr of a different but related type.
       *
       *  A pointer-to-Tp1 must be convertible to a
       *  pointer-to-Tp/element_type.
       *
       *  This object now @e owns the object previously owned by @a a,
       *  which has given up ownership.
       */
      template<typename _Tp1>
        auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }

      /**
       *  @brief  %auto_ptr assignment operator.
       *  @param  a  Another %auto_ptr of the same type.
       *
       *  This object now @e owns the object previously owned by @a a,
       *  which has given up ownership.  The object that this one @e
       *  used to own and track has been deleted.
       */
      auto_ptr&
      operator=(auto_ptr& __a) throw()
      {
	reset(__a.release());
	return *this;
      }

      /**
       *  @brief  %auto_ptr assignment operator.
       *  @param  a  Another %auto_ptr of a different but related type.
       *
       *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
       *
       *  This object now @e owns the object previously owned by @a a,
       *  which has given up ownership.  The object that this one @e
       *  used to own and track has been deleted.
       */
      template<typename _Tp1>
        auto_ptr&
        operator=(auto_ptr<_Tp1>& __a) throw()
        {
	  reset(__a.release());
	  return *this;
	}

      /**
       *  When the %auto_ptr goes out of scope, the object it owns is
       *  deleted.  If it no longer owns anything (i.e., @c get() is
       *  @c NULL), then this has no effect.
       *
       *  The C++ standard says there is supposed to be an empty throw
       *  specification here, but omitting it is standard conforming.  Its
       *  presence can be detected only if _Tp::~_Tp() throws, but this is
       *  prohibited.  [17.4.3.6]/2
       */
      ~auto_ptr() { delete _M_ptr; }
      
      /**
       *  @brief  Smart pointer dereferencing.
       *
       *  If this %auto_ptr no longer owns anything, then this
       *  operation will crash.  (For a smart pointer, <em>no longer owns
       *  anything</em> is the same as being a null pointer, and you know
       *  what happens when you dereference one of those...)
       */
      element_type&
      operator*() const throw() 
      {
	_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
	return *_M_ptr; 
      }
      
      /**
       *  @brief  Smart pointer dereferencing.
       *
       *  This returns the pointer itself, which the language then will
       *  automatically cause to be dereferenced.
       */
      element_type*
      operator->() const throw() 
      {
	_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
	return _M_ptr; 
      }
      
      /**
       *  @brief  Bypassing the smart pointer.
       *  @return  The raw pointer being managed.
       *
       *  You can get a copy of the pointer that this object owns, for
       *  situations such as passing to a function which only accepts
       *  a raw pointer.
       *
       *  @note  This %auto_ptr still owns the memory.
       */
      element_type*
      get() const throw() { return _M_ptr; }
      
      /**
       *  @brief  Bypassing the smart pointer.
       *  @return  The raw pointer being managed.
       *
       *  You can get a copy of the pointer that this object owns, for
       *  situations such as passing to a function which only accepts
       *  a raw pointer.
       *
       *  @note  This %auto_ptr no longer owns the memory.  When this object
       *  goes out of scope, nothing will happen.
       */
      element_type*
      release() throw()
      {
	element_type* __tmp = _M_ptr;
	_M_ptr = 0;
	return __tmp;
      }
      
      /**
       *  @brief  Forcibly deletes the managed object.
       *  @param  p  A pointer (defaults to NULL).
       *
       *  This object now @e owns the object pointed to by @a p.  The
       *  previous object has been deleted.
       */
      void
      reset(element_type* __p = 0) throw()
      {
	if (__p != _M_ptr)
	  {
	    delete _M_ptr;
	    _M_ptr = __p;
	  }
      }
      
      /** 
       *  @brief  Automatic conversions
       *
       *  These operations convert an %auto_ptr into and from an auto_ptr_ref
       *  automatically as needed.  This allows constructs such as
       *  @code
       *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
       *    ...
       *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
       *  @endcode
       */
      auto_ptr(auto_ptr_ref<element_type> __ref) throw()
      : _M_ptr(__ref._M_ptr) { }
      
      auto_ptr&
      operator=(auto_ptr_ref<element_type> __ref) throw()
      {
	if (__ref._M_ptr != this->get())
	  {
	    delete _M_ptr;
	    _M_ptr = __ref._M_ptr;
	  }
	return *this;
      }
      
      template<typename _Tp1>
        operator auto_ptr_ref<_Tp1>() throw()
        { return auto_ptr_ref<_Tp1>(this->release()); }

      template<typename _Tp1>
        operator auto_ptr<_Tp1>() throw()
        { return auto_ptr<_Tp1>(this->release()); }
    } _GLIBCXX_DEPRECATED;

  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 541. shared_ptr template assignment and void
  template<>
    class auto_ptr<void>
    {
    public:
      typedef void element_type;
    } _GLIBCXX_DEPRECATED;

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

#endif /* _BACKWARD_AUTO_PTR_H */


Unity资源逆向解析:从APK到AssetStudio的完整实战 本文详细介绍了Unity资源逆向解析的全流程,从APK解包到使用AssetStudio深度解析Unity游戏资源。通过实战案例演示如何提取《植物大战僵尸》等游戏的素材,包括贴图、音频和3D模型,帮助开发者掌握Unity游戏逆向工程的核心技术。 阅读详情

相关推荐

Vue项目实战:海康WebControl插件从安装到预览功能全流程(附RSA加密避坑指南)

本文详细介绍了在Vue项目中集成海康威视WebControl插件的全流程,从环境准备、插件部署到核心功能实现。重点解析了使用JS_RequestInterface获取RSA公钥并进行加密参数初始化的关键步骤与常见避坑指南,帮助开发者高效完成监控视频预览功能的开发与调试。

linux6sysadmin的博客 1236

C++智能指针auto_ptr、unique_ptr、shared_ptr、weak_prt详解

C++智能指针auto_ptr、unique_ptr、shared_ptr、weak_prt详解

GodFishhh的博客 2469

SYSU-MM01数据集评估避坑指南:从RGB/IR图像理解到Python评估脚本调优

本文详细解析了SYSU-MM01数据集在跨模态行人重识别(ReID)评估中的关键技巧与常见陷阱。从RGB/IR图像理解到Python评估脚本优化,提供了数据评估模式选择、single-shot/multi-shot差异分析、特征文件处理规范等实用指南,帮助研究者避开指标异常问题,提升评估效率与准确性。

weixin_29044713的博客 198

[C++11] 智能指针(auto_ptr(弃用)、unique_ptr、shared_ptr、weak_ptr)详细解读

这里主要解释auto_ptr、unique_ptr、shared_ptr、weak_ptr这几种智能指针。接下来我们对每一个指针类型进行详细说明。在 C++ 中,auto_ptr、unique_ptr、shared_ptr 和 weak_ptr四种智能指针主要用于管理动态分配的对象的生命周期。它们在所有权管理和内存安全方面有所不同。

wangdsh的博客 2569

【技术分享】语音多模态LLM越狱

语音多模态LLM越狱攻击作为一种新兴安全威胁,揭示了多模态模型在语音输入场景下的脆弱性。尽管已有多种攻击方法被提出,但其具体实现步骤、防御措施的有效性及行业影响等问题仍需进一步探索[9]。未来研究应聚焦于改进模型架构、开发针对性防御策略,并评估语音输入越狱攻击的实际危害。只有通过全面理解这些挑战,才能为多模态智能系统的安全性提供更可靠的保障。说到语音多模态LLM越狱的具体实现步骤,这其实是一个复杂且多层次的过程🧐。首先,攻击者需要深入分析目标模型的响应模式和安全机制,找出可能的漏洞点。

qq_29883477的博客 1267

SPSS篇—卡方检验

今天依旧跟大家分享一个在SPSS中使用率比较高的分析方法:卡方检验。 在开始做分析之前,我们需要明白两件事情:卡方检验是什么?一般用来干什么?我们只有充分了解分析方法以后才能够正确的使用它。 卡方检验在百科中的解释是:卡方检验是用途非常广的一种假设检验方法,它在分类资料统计推断中的应用,包括:两个率或两个构成比比较的卡方检验;多个率或多个构成比比较的卡方检验以及分类资料的相关分析等。它的原理是...

小白数据营的博客 20万+

智能指针类模板:auto_ptr、unique_ptr、shared_ptr的原理与使用

本文介绍了智能指针类模板的原理与使用,其中主要介绍了auto_ptr、unique_ptr、shared_ptr,它们有各自的特性,适用于不同的场景,同时也各有优点和缺陷。实际使用时要结合其特点进行选择。

baidu_38797690的博客 1514

Linux系统安全学习手册

Linux反弹shell Linux系统应急响应 Linux服务器安全加固 Linux下的用户、组和权限 Linux中的gcc Linux中su和sudo的用法和区别 Linux系统python2与python3共存 Linux中grep工具的使用 Linux中环境变量的设置 Linux中常见的150个命令(干货) Linux中编写Shell脚本 LNMP环境搭建Wordpress博客 LAMP环境搭建一个Discuz论坛 Linux运维比较常用的一些脚本 Linux系统中安装软

谢公子的博客 7541

auto_ptr作用

auto_ptr作用 auto_ptr的出现,主要是为了解决“有异常抛出时发生内存泄漏”的问题;抛出异常,将导致指针p所指向的空间得不到释放而导致内存泄漏; auto_ptr构造时取得某个对象的控制权,在析构时释放该对象。我们实际上是创建一个auto_ptr类型的局部对象,该局部对象析构时,会将自身所拥有的指针空间释放,所以不会有内存泄漏; auto_ptr的构造函数是explicit,阻止了一般指针隐式转换为 auto_ptr的构造,所以不能直接将一般类型的指针赋值给auto_ptr类型的对象

chuxuezhe8483的博客 793

环境配置与基础教程:2026大厂标准:使用 DVC (Data Version Control) 实现 YOLO 数据集版本控制全链路管理

本文介绍了使用DVC 3.0对YOLO数据集进行全链路版本控制的方法。主要内容包括: DVC 3.0的核心增强:内容块级缓存去重、并行云传输、AI智能体元数据跟踪等新特性,可显著提升YOLO数据集管理效率。 环境配置:详细说明DVC 3.0的安装步骤、项目初始化方法以及远程存储配置方案(支持S3、GCS、Azure等多种存储后端)。 目录结构规范:提供符合大厂MLOps标准的项目目录结构设计。 实战操作: 原始数据版本化 多格式标注数据版本控制 版本切换方法 优势说明:DVC通过哈希指针机制管理大型数据集,

ZXSXJ的博客 153

SAP 与 Palantir 合作全景解析:这不是普通集成,而是把 ERP、数据语义层、云迁移与企业级 AI 串成一条价值链

SAP与Palantir的战略合作聚焦企业数字化转型核心痛点,通过深度技术整合构建"数据-决策-执行"闭环。合作包含四大主线:云迁移连续性保障、SAP业务数据云与Palantir平台对接、联合工程开发、高监管行业优先落地。技术实现上采用SAP认证插件,支持双向数据交互和实时业务动作触发。该合作突破了传统ERP系统局限,将业务语义与AI执行相结合,特别适合制造业供应链优化、财务争议处理等高价值场景,为企业在云迁移过程中实现业务连续性与智能化升级提供新范式。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 203

C++智能指针的使用,auto_ptr,unique_ptr,shared_ptr,weak_ptr详解

C++智能指针的用法,auto_ptr,unique_ptr,shared_ptr,weak_ptr

liu20000505的博客 2314

车载网络测试实操源码_使用CAPL解析VBF文件

VBF(Versatile Binary Format)文件是一种二进制文件格式,被沃尔沃、福特、马自达、吉利等汽车制造商广泛采用。这种格式无法使用我们常规的烧录器进行刷写,通常需要进行解析转化。如果各位需要自己编写软件或脚本来刷写VBF文件,或者需要对刷写功能进行测试时,那么本篇文章就会对你非常有用。今天我们就来讲一下如何使用CAPL脚本对VBF文件进行数据解析。VBF文件包含三部分:VBF版本段、文件头段、数据段。前面的版本段和文件头段是ASCII码,包含版本、地址、校验码等信息

文章主要涉及嵌入式软件开发、测试、项目管理等,欢迎交流,共同进步。 3013

C++智能指针详解(auto_ptr、unique_ptr、shared_ptr)

智能指针的应用场景、智能指针的介绍、智能指针的使用与原理、auto_ptr、unique_ptr、shared_ptr、shared_ptr与线程安全、shared_ptr的循环引用、weak_ptr解决循环引用、智能指针与定制删除器、lock与RAII、内存泄漏

wuqiongjin的博客 2561

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C++智能指针——auto_ptr、unique_ptr和shared_ptr详解

一、为什么使用智能指针? 1.1 内存泄漏 对于C/C++程序员来说,内存泄漏往往是加班或调试程序的罪魁祸首。 什么是内存泄漏呢? 内存泄漏是指因为疏忽或错误造成程序未能释放已经不再使用的内存的情况。简单来说,就是一块内存我们已经失去了对该段内存的控制。 内存泄漏不是指物理结构上的消失。 内存泄漏的危害? 长期运行的程序出现内存泄漏,影响很大,如操作系统、后台服务等等,出现内存泄漏会导致响应越来越慢,最终卡死。 使用智能指针,通常是因为以下两个场景,用一段代码模拟演示: 问题分析: ne

1273

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

C++智能指针auto_ptr详解

auto_ptrauto_ptr是C++标准库中()为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针)auto_ptr的实现原理其实就是RAII,在构造的时候获取资源,在析构的时候释放资源,并进行相关指针操作的重载,使用起来就像普通的指针。std::auto_ptr<ClassA> pa(new ClassA);但是由于其构造函数声明为explicit的,因此不能通过饮

OSKernelLAB(gatieme) 1万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C++智能指针:auto_ptr详解

指针,相信大家并不陌生。无论是我们在进行查看内存还是在修改字符串,我们都会用到指针。 最常见的情况则是我们使用malloc或者new申请到了一块内存,然后用一个指针来保存起来。我们都知道有内存的申请那就必须要对它进行释放的处理,否则会造成最严重的后果——内存泄漏。一个或者两个申请的内存我们或许不会忘记去释放,但是如果成千上万行代码,你还能记得住哪个释放了哪个没有释放吗? 而智能指针就是为了解决...

Eyoru的博客 1万+

C++11智能指针(auto_ptr,unique_ptr,shared_ptr,weak_ptr的详解与模拟实现)

概念:RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内存、文件句柄、网络连接、互斥量等等)的简单技术。在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大好处:不需要显式地释放资源。采用这种方式,对象所需的资源在其生命期内始终保持有效。

yin_ming_hui的博客 1102

C++智能指针(auto_ptr)详解

1. auto_ptr是什么? auto_ptr 是C++标准库提供的类模板,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同时被分给两个拥有者。当auto_ptr对象生命周期结束时,其析构函数会将auto_ptr对象拥有的动态内存自动释...

chijiaer4384的博客 212

c++智能指针之auto_ptr详解(有源码有实例)

前言 内存泄漏大概是每一个c/c++程序员最深恶痛绝的问题,因为大部分此类问题都是令广大c程序员很抓狂,掉头发的疑难杂症。而内存泄漏的根本原因就是指针的使用不当引起的,例如指针指向的内存没有释放,导致产生了程序无法控制的内存块,而随着程序不断执行,这样的内存越积越多,最终导致程序使用的内存空间不够导致宕机等一些严重的后果。为了解决这种让人讨厌的问题,c++提供了一系列的智能指针例如auto_ptr...

weixin_43055404的博客 906

c++  auto_ptr智能指针详解

从一个小例子说起: 一般定义的raw指针是这样的使用 class CBase; class Derive : public CBase { CBase *pa = new Derive;//如果这里发生异常 比如没有足够的内存分配 pa->调用相关成员函数;// delete pa;//则不会执行到这里的删除指针内存的操作 发生内存泄漏 } 这

_Rad 的专栏 1449

详解C++各种智能指针: auto_ptr, shared_ptr, weak_ptr, scoped_ptr

C++ 中经常被问到的一个问题就是如何分辨各种智能指针。  先归纳起来做一个综合的比较:  auto_ptr auto_ptr是现在标准库里面一个轻量级的智能指针的实现,存在于头文件 memory中,之所以说它是轻量级,是因为它只有一个成员变量(拥有对象的指针),相关的调用开销也非常小。auto_ptr在03标准中出现,是最早的C++标准库里的智能指针,主要是通过将指针通

elfprincexu的专栏 1万+

c++基础知识之智能指针一:auto_ptr详解

背景介绍: RAll机制 定义一个类来封装资源的分配和释放,在构造函数中完成资源的分配和初始化,在析构函数中完成资源的清理,从而保证资源的正确初始化和清理 ps:智能指针就是RAll机制的一种应用,智能指针从根本上来说是一个对象 1.auto_ptr是什么? auto_ptr是C++标准库提供的类模板,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同时被分配给两个拥有者,当auto_ptr对象生命周期结束时,其析构函数会将auto...

山水之间 758

C++智能指针全套详解:auto_ptr/unique_ptr/shared_ptr/weak_ptr原理、引用计数、循环引用缺陷、内存泄漏根治与工程选型

weak_ptr 不参与计数,对象销毁时计数可以正常归零,彻底打破循环引用闭环,根治泄漏。默认智能指针只支持普通堆内存,对于数组、文件指针、套接字需要自定义删除器。// 文件自动关闭删除器彻底通关 C++智能指针全套体系,解决工程90%内存问题:✅ 裸指针四大致命缺陷与智能指针设计初衷✅ auto_ptr废弃底层原因与避坑准则✅ unique_ptr独占机制、移动语义、工程首选场景✅ shared_ptr引用计数底层原理、共享模型✅ 循环引用内存泄漏完整复现与根治方案。

2401_86195313的博客 350
上一篇: C++模板与泛型编程
下一篇: C++智能指针——unique_ptr详解
OODeveloper
博客等级 码龄12年 43粉丝 137原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址