A TR1 Tutorial: Smart Pointers

本文探讨了C++标准库中智能指针的发展,从auto_ptr的问题出发,介绍了TR1中新增的shared_ptr和weak_ptr特性。shared_ptr通过引用计数解决了资源所有权转移的问题,并能用于STL容器;weak_ptr则用于解决共享指针可能导致的循环引用问题。

Until TR1, the only smart pointer available in the standard library was auto_ptr; that presents some major disadvantages because of its exclusive ownership model. To address these issues, the new standard offers two new implementations, shared_ptr and weak_ptr, both defined in header <memory>.

Class std::auto_ptr

As mentioned earlier, auto_ptr is based on an exclusive ownership model; each means that two pointers (of this type) cannot point to the same resource. Copying or assigning makes the resource changing its owner, with the source giving the ownership to the destination.

#include <memory>
#include <iostream>

class foo
{
public:
   void print() {std::cout << "foo::print" << std::endl;}
};

int main()
{
   std::auto_ptr<foo> ap1(new foo);
   ap1->print();
   std::cout << "ap1 pointer: " << ap1.get() << std::endl;

   std::auto_ptr<foo> ap2(ap1);
   ap2->print();
   std::cout << "ap1 pointer: " << ap1.get() << std::endl;
   std::cout << "ap2 pointer: " << ap2.get() << std::endl;

   return 0;
}

The output is:

foo::print
ap1 pointer: 0033A790
foo::print
ap1 pointer: 00000000
ap2 pointer: 0033A790

The exact value of the wrapped pointer (0033A790) is not important. The issue here is that, after creating and initializing object ap2, ap1 gave up the ownership of the resource, and its wrapper pointer became NULL.

The major problems introduced by auto_ptr are:

  • Copying and assigning changes the owner of a resource, modifying not only the destination but also the source, which it not intuitive.
  • It cannot be used in STL containers because the constraint that a container's elements must be copy constructable and assignable does not apply to this class.

What's New in TR1?

Two new smart pointers were added to the standard template library:

  • shared_ptr: Based on a reference counter model, with the counter incremented each time a new shared pointer object points to the resource, and decremented when the object's destructor executes; when the counter gets to 0, the resource is released. This pointer is copy constructable and assignable; this makes it usable in STL containers. Moreover, the shared pointer works with polymorphic types and incomplete types. Its major drawback is the impossibility to detect cyclic dependencies, in which case the resources never get released (for example, a tree with nodes having (shared) pointers to children but also to the parent, in which case the parent and the children are referencing each other, in a cycle). To fix this issue, a second smart pointer was created:
  • weak_ptr: Points to a resource referred by a shared pointer, but does not participate in reference counting. When the counters gets to 0, the resource is released, regardless the number of weak pointers referring it; all these pointers are marked as invalid.

The next example shows a similar implementation to the first example, replacing auto_ptr with shared_ptr.

int main()
{
   std::tr1::shared_ptr<foo> sp1(new foo);
   sp1->print();
   std::cout << "sp1 pointer: " << sp1.get() << std::endl;

   std::tr1::shared_ptr<foo> sp2(sp1);
   sp2->print();
   std::cout << "sp1 pointer: " << sp1.get() << std::endl;
   std::cout << "sp2 pointer: " << sp2.get() << std::endl;

   std::cout << "counter sp1: " << sp1.use_count() << std::endl;
   std::cout << "counter sp2: " << sp2.use_count() << std::endl;

   return 0;
}
foo::print
sp1 pointer: 0033A790
foo::print
sp1 pointer: 0033A790
sp2 pointer: 0033A790
counter sp1: 2
counter sp2: 2

As you can see, when sp2 is created, sp1 does not give up the ownership, changing its wrapped pointer to NULL; it only increments the reference counter. When the two shared pointer objects get out of scope, the last one that is destroyed will release the resource.

内容概要:本文档围绕“经济学期刊论文复现:数字化转型能否促进企业的高质量发展”这一核心命题,系统整合了MATLAB与Python编程实现的大量科研案例,聚焦于数字化转型对企业全要素生产率(TFP)及高质量发展影响的实证研究。文档不仅复现了高水平经济学期刊论文中的计量经济模型,如基于中国上市公司数据的数字化转型与生产率关系分析,还深度融合了工程领域的建模技术,涵盖微电网优化、负荷预测、风电光伏不确定性建模、电力系统故障仿真等。同时,提供了智能优化算法(如遗传算法、粒子群优化)、机器学习(LSTM、CNN-BiGRU-Attention)、信号处理、路径规划等多学科交叉的技术资源,构建了一个从理论推导到代码实现的完整科研支持体系,旨在帮助研究者系统掌握论文复现与实证分析的核心方法。; 适合人群:具备一定MATLAB或Python编程基础,从事经济学、管理学、能源系统、智能制造及相关交叉学科研究的研究生、科研人员及高校教师。; 使用场景及目标:①复现经济学顶刊中关于数字化转型与企业高质量发展的实证模型;②学习如何量化数字化转型并构建其对企业绩效的影响评估框架;③掌握基于真实数据的计量经济建模、场景生成与优化调度仿真技术,全面提升科研论文写作与实证研究能力。; 阅读建议:建议读者结合文中提供的代码与数据资源,重点研读“论文复现”与“创新未发表”模块,按照技术路径循序渐进地实现模型复现与拓展。推荐关注“荔枝科研社”公众号及百度网盘链接获取完整资料,系统性地开展学习与科研实践。
下载代码方式:https://pan.quark.cn/s/9de6a9d0b3d8 依据所提供的文件内容,能够推导出此段程序的核心任务在于对一个任意的三位数进行拆解,并且分别呈现该数值的百位、十位及个位部分。随后,我们将对该知识点进行进一步的深入研究。 ### 一、程序功能说明 #### 1. 接收任意一个三位数输入 程序起始阶段运用`scanf`函数来获取用户输入的一个整数。为确保输入内容确实为一个三位数,在实际应用场景中通常需要嵌入验证机制来保障输入的有效性。然而,在本示例情形下,该环节被简化处理,预设用户总会准确输入一个三位数。 #### 2. 实施数字的拆分并提取各位置数值 程序借助一系列数学计算来对三位数进行拆分,将其转化为百位、十位和个位三个独立的构成部分。具体而言,通过除法和取模运算完成了这一过程。 #### 3. 展示各位置上的数值 程序运用`printf`函数来输出原始数值以及各个位上的数值。需要留意的是,代码中的输出部分似乎存在一些混淆,存在语法上的错误,例如多余的`printf`语句和乱码字符等问题。 ### 二、核心代码分析 #### 1. 数字拆分逻辑 ```c a[0] = n / 1000; // 提取千位数,但鉴于题目要求是三位数,此处应为百位数 a[1] = n % 1000 / 100; // 提取百位数 a[2] = n % 1000 % 100 / 10; // 提取十位数 a[3] = n % 1000 % 100 % 10; // 提取个位数 ``` 这段代码通过一连串的除法和取模运算,成功地将输入的数字n拆分为百位、十位和个位三个独立的构成部分,...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值