Smart Pointers - What, Why, Which?

简介: Smart Pointers - What, Why, Which?What are they?Smart pointers are objects that look and feel like pointers, but are smarter.

Smart Pointers - What, Why, Which?


What are they?

Smart pointers are objects that look and feel like pointers, but are smarter. What does this mean?

To look and feel like pointers, smart pointers need to have the same interface that pointers do: they need to support pointer operations like dereferencing (operator *) and indirection (operator ->). An object that looks and feels like something else is called a proxy object, or just proxy. The proxy pattern and its many uses are described in the books Design Patterns and Pattern Oriented Software Architecture.

To be smarter than regular pointers, smart pointers need to do things that regular pointers don't. What could these things be? Probably the most common bugs in C++ (and C) are related to pointers and memory management: dangling pointers, memory leaks, allocation failures and other joys. Having a smart pointer take care of these things can save a lot of aspirin...

The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. You can find it in the header <memory>, or take a look at Scott Meyers' auto_ptr implementation. Here is part of auto_ptr's implementation, to illustrate what it does:

template <class T> class auto_ptr
{
    T* ptr;
public:
    explicit auto_ptr(T* p = 0) : ptr(p) {}
    ~auto_ptr()                 {delete ptr;}
    T& operator*()              {return *ptr;}
    T* operator->()             {return ptr;}
    // ...
};
As you can see, auto_ptr is a simple wrapper around a regular pointer. It forwards all meaningful operations to this pointer (dereferencing and indirection). Its smartness in the destructor: the destructor takes care of deleting the pointer.

For the user of auto_ptr, this means that instead of writing:

void foo()
{
    MyClass* p(new MyClass);
    p->DoSomething();
    delete p;
}
You can write:
void foo()
{
    auto_ptr<MyClass> p(new MyClass);
    p->DoSomething();
}
And trust p to cleanup after itself.

What does this buy you? See the next section.

Why would I use them?

Obviously, different smart pointers offer different reasons for use. Here are some common reasons for using smart pointers in C++.

Why: Less bugs

Automatic cleanup. As the code above illustrates, using smart pointers that clean after themselves can save a few lines of code. The importance here is not so much in the keystrokes saved, but in reducing the probability for bugs: you don't need to remember to free the pointer, and so there is no chance you will forget about it.

Automatic initialization. Another nice thing is that you don't need to initialize the auto_ptr to NULL, since the default constructor does that for you. This is one less thing for the programmer to forget.

Dangling pointers. A common pitfall of regular pointers is the dangling pointer: a pointer that points to an object that is already deleted. The following code illustrates this situation:

MyClass* p(new MyClass);
MyClass* q = p;
delete p;
p->DoSomething();   // Watch out! p is now dangling!
p = NULL;           // p is no longer dangling
q->DoSomething();   // Ouch! q is still dangling!
For auto_ptr, this is solved by setting its pointer to NULL when it is copied:
template <class T>
auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<T>& rhs)
{
    if (this != &rhs) {
        delete ptr;
        ptr = rhs.ptr;
        rhs.ptr = NULL;
    }
    return *this;
}
Other smart pointers may do other things when they are copied. Here are some possible strategies for handling the statement q = p, where p and q are smart pointers:
  • Create a new copy of the object pointed by p, and have q point to this copy. This strategy is implemented in copied_ptr.h.
  • Ownership transfer: Let both p and q point to the same object, but transfer the responsibility for cleaning up ("ownership") from p to q. This strategy is implemented in owned_ptr.h.
  • Reference counting: Maintain a count of the smart pointers that point to the same object, and delete the object when this count becomes zero. So the statement q = p causes the count of the object pointed by p to increase by one. This strategy is implemented in counted_ptr.h. Scott Meyers offers another reference counting implementation in his book More Effective C++.
  • Reference linking: The same as reference counting, only instead of a count, maintain a circular doubly linked list of all smart pointers that point to the same object. This strategy is implemented in linked_ptr.h.
  • Copy on write: Use reference counting or linking as long as the pointed object is not modified. When it is about to be modified, copy it and modify the copy. This strategy is implemented in cow_ptr.h.
All these techniques help in the battle against dangling pointers. Each has each own benefits and liabilities. The Which section of this article discusses the suitability of different smart pointers for various situations.

Why: Exception Safety

Let's take another look at this simple example:
void foo()
{
    MyClass* p(new MyClass);
    p->DoSomething();
    delete p;
}
What happens if DoSomething() throws an exception? All the lines after it will not get executed and p will never get deleted! If we're lucky, this leads only to memory leaks. However, MyClass may free some other resources in its destructor (file handles, threads, transactions, COM references, mutexes) and so not calling it my cause severe resource locks.

If we use a smart pointer, however, p will be cleaned up whenever it gets out of scope, whether it was during the normal path of execution or during the stack unwinding caused by throwing an exception.

But isn't it possible to write exception safe code with regular pointers? Sure, but it is so painful that I doubt anyone actually does this when there is an alternative. Here is what you would do in this simple case:

void foo()
{
    MyClass* p;
    try {
        p = new MyClass;
        p->DoSomething();
        delete p;
    }
    catch (...) {
        delete p;
        throw;
    }
}
Now imagine what would happen if we had some if's and for's in there...

Why: Garbage collection

Since C++ does not provide automatic garbage collection like some other languages, smart pointers can be used for that purpose. The simplest garbage collection scheme is reference counting or reference linking, but it is quite possible to implement more sophisticated garbage collection schemes with smart pointers. For more information see the garbage collection FAQ.

Why: Efficiency

Smart pointers can be used to make more efficient use of available memory and to shorten allocation and deallocation time.

A common strategy for using memory more efficiently is copy on write (COW). This means that the same object is shared by many COW pointers as long as it is only read and not modified. When some part of the program tries to modify the object ("write"), the COW pointer creates a new copy of the object and modifies this copy instead of the original object. The standard string class is commonly implemented using COW semantics (see the <string> header).

string s("Hello");

string t = s;       // t and s point to the same buffer of characters

t += " there!";     // a new buffer is allocated for t before
                    // appending " there!", so s is unchanged.

Optimized allocation schemes are possible when you can make some assumptions about the objects to be allocated or the operating environment. For example, you may know that all the objects will have the same size, or that they will all live in a single thread. Although it is possible to implement optimized allocation schemes using class-specific new and delete operators, smart pointers give you the freedom to choose whether to use the optimized scheme for each object, instead of having the scheme set for all objects of a class. It is therefore possible to match the allocation scheme to different operating environments and applications, without modifying the code for the entire class.

Why: STL containers

The C++ standard library includes a set of containers and algorithms known as the standard template library (STL). STL is designed to be generic (can be used with any kind of object) and efficient (does not incur time overhead compared to alternatives). To achieve these two design goals, STL containers store their objects by value. This means that if you have an STL container that stores objects of class Base, it cannot store of objects of classes derived from Base.
class Base { /*...*/ };
class Derived : public Base { /*...*/ };

Base b;
Derived d;
vector<Base> v;

v.push_back(b); // OK
v.push_back(d); // error
What can you do if you need a collection of objects from different classes? The simplest solution is to have a collection of pointers:
vector<Base*> v;

v.push_back(new Base);      // OK
v.push_back(new Derived);   // OK too

// cleanup:
for (vector<Base*>::iterator i = v.begin(); i != v.end(); ++i)
    delete *i;
The problem with this solution is that after you're done with the container, you need to manually cleanup the objects stored in it. This is both error prone and not exception safe.

Smart pointers are a possible solution, as illustrated below. (An alternative solution is a smart container, like the one implemented in pointainer.h.)

vector< linked_ptr<Base> > v;
v.push_back(new Base);      // OK
v.push_back(new Derived);   // OK too

// cleanup is automatic
Since the smart pointer automatically cleans up after itself, there is no need to manually delete the pointed objects.

Note: STL containers may copy and delete their elements behind the scenes (for example, when they resize themselves). Therefore, all copies of an element must be equivalent, or the wrong copy may be the one to survive all this copying and deleting. This means that some smart pointers cannot be used within STL containers, specifically the standard auto_ptr and any ownership-transferring pointer. For more info about this issue, see C++ Guru of the Week #25.

Which one should I use?

Are you confused enough? Well, this summary should help.

Which: Local variables

The standard auto_ptr is the simplest smart pointer, and it is also, well, standard. If there are no special requirements, you should use it. For local variables, it is usually the right choice.

Which: Class members

Although you can use auto_ptr as a class member (and save yourself the trouble of freeing objects in the destructor), copying one object to another will nullify the pointer, as illustrated Below.
class MyClass
{
    auto_ptr<int> p;
    // ...
};

MyClass x;
// do some meaningful things with x
MyClass y = x; // x.p now has a NULL pointer
Using a copied pointer instead of auto_ptr solves this problem: the copied object (y) gets a new copy of the member.

Note that using a reference counted or reference linked pointer means that if y changes the member, this change will also affect x! Therefore, if you want to save memory, you should use a COW pointer and not a simple reference counted/linked pointer.

Which: STL containers

As explained above, using garbage-collected pointers with STL containers lets you store objects from different classes in the same container.

It is important to consider the characteristics of the specific garbage collection scheme used. Specifically, reference counting/linking can leak in the case of circular references (i.e., when the pointed object itself contains a counted pointer, which points to an object that contains the original counted pointer). Its advantage over other schemes is that it is both simple to implement and deterministic. The deterministic behavior may be important in some real time systems, where you cannot allow the system to suddenly wait while the garbage collector performs its housekeeping duties.

Generally speaking, there are two ways to implement reference counting: intrusive and non-intrusive. Intrusive means that the pointed object itself contains the count. Therefore, you cannot use intrusive reference counting with 3-rd party classes that do not already have this feature. You can, however, derive a new class from the 3-rd party class and add the count to it. Non-intrusive reference counting requires an allocation of a count for each counted object. The counted_ptr.h is an example of non-intrusive reference counting.

Reference linking does not require any changes to be made to the pointed objects, nor does it require any additional allocations. A reference linked pointer takes a little more space than a reference counted pointer - just enough to store one or two more pointers.

Both reference counting and reference linking require using locks if the pointers are used by more than one thread of execution.

Which: Explicit ownership transfer

Sometimes, you want to receive a pointer as a function argument, but keep the ownership of this pointer (i.e. the control over its lifetime) to yourself. One way to do this is to use consistent naming-conventions for such cases. Taligent's Guide to Designing Programs recommends using "adopt" to mark that a function adopts ownership of a pointer.

Using an owned pointer as the function argument is an explicit statement that the function is taking ownership of the pointer.

Which: Big objects

If you have objects that take a lot of space, you can save some of this space by using COW pointers. This way, an object will be copied only when necessary, and shared otherwise. The sharing is implemented using some garbage collection scheme, like reference counting or linking.

Which: Summary

For this: Use that:
Local variables auto_ptr
Class members Copied pointer
STL Containers Garbage collected pointer (e.g. reference counting/linking)
Explicit ownership transfer Owned pointer
Big objects Copy on write

Conclusion

Smart pointers are useful tools for writing safe and efficient code in C++. Like any tool, they should be used with appropriate care, thought and knowledge. For a comprehensive and in depth analysis of the issues concerning smart pointers, I recommend reading Andrei Alexandrescu's chapter about smart pointers in his book Modern C++ Design.

Feel free to use my own smart pointers in your code.
The Boost C++ libraries include some smart pointers, which are more rigorously tested and actively maintained. Do try them first, if they are appropriate for your needs.

目录
相关文章
|
机器学习/深度学习 存储 人工智能
第3章:知识表示:概述、符号知识表示、向量知识表示
第3章:知识表示:概述、符号知识表示、向量知识表示
第3章:知识表示:概述、符号知识表示、向量知识表示
|
2月前
|
数据采集 人工智能 测试技术
从被搜索到被引用:GEO 优化深水区的底层逻辑与落地路径
2026年,流量入口正从“位置竞争”转向“语义竞争”:用户在对话框中直接获取答案,企业比拼的是被AI引用而非搜索排名。GEO(生成式引擎优化)应运而生——通过结构化、可验证、事实一致的公开内容,提升AI在回答中准确署名你的概率。它不替代SEO,而是构建第二层可见度。
|
2月前
|
人工智能 JavaScript 前端开发
AGENTS.md 实战指南:如何让 AI 编程助手真正遵循项目规范
本文介绍 `AGENTS.md`——一份专为AI编程助手设计的项目规范指南。它通过Markdown统一记录技术栈、目录结构、编码约定、禁止事项等,帮助Cursor、Claude Code等工具生成更符合项目实际的代码,减少类型滥用、路径错乱、违规调用等问题,提升AI辅助开发的准确性与工程可控性。(239字)
|
2月前
|
人工智能 自然语言处理 小程序
阿里云万小智最新3.0版本,首月低至15元,建站+经营,支持GEO/SEO/内容分发
阿里云万小智AI建站是原云·速成美站的升级产品,融合AI生成与零代码理念,凭自然语言对话与拖拽即可快速建站。3.0版本将能力从建站延伸至经营,新增应用诊断优化、GEO/SEO原生内容创作与多渠道内容分发,定位为AI建站与经营平台。产品分Web与小程序两类,各有轻量、标准、高级三档,价格360/980/1980元每年,轻量版首月低至15元。高端定制可选云·企业官网,两者互补覆盖全场景。
|
人工智能 监控 安全
落地之道丨突破效率桎梏,系统性构建 Agentic 组织
智能体组织的本质,不在于部署了多少 AI 工具,而在于是否将 AI 能力系统性地写入了组织的架构、流程、知识体系与文化基因。
落地之道丨突破效率桎梏,系统性构建 Agentic 组织
|
3月前
|
缓存 人工智能 JSON
中小厂云MES落地:一套能跑通的 PLC / ERP 集成架构
本文基于中小制造企业数字化实践,剖析MES项目失败主因——边界不清,并提出以ISA-95为纲的分层架构:边缘网关终结工业协议、云MES专注业务逻辑、ERP与MES通过3个核心接口解耦。强调“设备数据→业务语言”的翻译本质,给出可落地的数据模型、断网缓存方案及实施路径。
|
5月前
|
消息中间件 小程序 前端开发
小程序平台云上架构搭建实战:如何突破原生支付 30% 分账限制,构建合规交易资金链路
越来越多撮合型、本地生活、共享设备类创业团队选择基于阿里云搭建自研小程序平台。团队在完成前端开发、云上业务架构部署、支付基础链路对接后,往往会遇到一个共性瓶颈:微信、支付宝原生分账接口存在 30% 金额上限约束。对于需要向入驻商家、服务商、场地合作方分配高额收益的平台而言,该限制严重制约业务扩张,同时私户转账补差的替代方案持续滋生 “二清” 与税务风险。 本文基于阿里云技术栈,完整介绍小程序平台分层架构设计、云上部署方案、标准交易支付链路;重点拆解原生分账的底层约束,客观对比三类分账落地路线,讲解银行存管式一清分账架构如何突破比例限制,为小程序技术负责人、架构师提供可落地的选型参考。关键词:阿
437 3
|
10月前
|
算法 定位技术 数据处理
【卫星】全球导航卫星系统GNSS中的欺骗与欺骗检测算法,通过IMU+GNSS融合定位,最终实现欺骗检测与结果分析附matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 🍎 往期回顾关注个人主页:Matlab科研工作室 👇 关注我领取海量matlab电子书和数学建模资料 🍊个人信条:格物致知,完整Matlab代码获取及仿真咨询内容私信。 🔥 内容介绍 一、核心背景:GNSS 欺骗的危害与检测需求 全球导航卫星系统(GNSS,如 GPS、北斗、GLONASS)是载体(无人机、车辆、船舶)定位导航的核心依赖,但欺骗攻击通过伪造虚假卫星信号,可诱导载体接收错误的位置、速度、时间(PVT)信息,导致载体偏离预定轨迹,造成严重安全隐患
618 2
|
5月前
|
人工智能 数据挖掘 数据安全/隐私保护
不会写提示词?用 4 个要素把 AI 用进日常办公
以会议纪要、周报和邮件为例,介绍用“对象、材料、要求、输出格式”四个要素编写办公提示词,并提供可复用模板与数据安全核对清单。
|
5月前
|
人工智能 安全 API
自媒体多账号管理工具选型核心:安全风控、任务并发与API限流适配
自媒体矩阵扩张至30账号、10平台时,管理效率骤降、风控风险飙升。本文从安全隔离(IP/指纹/权限)、任务并发(账号池/自动调度)与API限流适配(频率控制/熔断降级)三大维度,系统解析企业级多账号管理工具的科学选型逻辑。(239字)