Home  >  Article  >  Backend Development  >  C++ smart pointers: a comprehensive analysis of their life cycle

C++ smart pointers: a comprehensive analysis of their life cycle

WBOY
WBOYOriginal
2024-05-09 11:06:02737browse

C Life cycle of smart pointers: Creation: Smart pointers are created when memory is allocated. Ownership transfer: Transfer ownership through a move operation. Release: Memory is released when a smart pointer goes out of scope or is explicitly released. Object destruction: When the pointed object is destroyed, the smart pointer becomes an invalid pointer.

C++ 智能指针:全面剖析其生命周期

C Smart pointer: comprehensive analysis of its life cycle

Introduction

Smart pointer A pointer is a special pointer used in C to manage dynamically allocated memory. Unlike raw pointers, smart pointers are responsible for tracking the memory state of the object they point to and automatically releasing that memory when the object is no longer needed. This helps prevent common programming errors such as memory leaks and dangling pointers.

Types

The C standard library provides four main types of smart pointers:

  • ##unique_ptr8742468051c85b06f0a0af9e3e506b5c:Unique ownership pointer. Only one unique_ptr can point to an object at a time.
  • shared_ptr8742468051c85b06f0a0af9e3e506b5c: Pointer to shared ownership. There can be multiple shared_ptr pointing to the same object.
  • weak_ptr8742468051c85b06f0a0af9e3e506b5c: Weak reference pointer. weak_ptr does not prevent the object from being destroyed and needs to be used with shared_ptr.
  • auto_ptr8742468051c85b06f0a0af9e3e506b5c: Deprecated. Removed in C 11.

Lifecycle

1. Creation

Smart pointers can be created when the object allocates memory, like Same as using raw pointers:

auto ptr = std::make_unique<int>(42);

2. Ownership transfer

Smart pointers can transfer ownership through move operations:

auto ptr2 = std::move(ptr);  // ptr2 现在拥有对整数对象的唯一所有权

3. Release

When a smart pointer leaves its scope or is explicitly released, it will release the memory it owns:

{
    auto ptr = std::make_unique<int>(42);
    // ...
}  // ptr 在此处释放

4. Object destruction

When the object pointed to is destroyed, the smart pointer will become an invalid pointer:

int* ptr = new int(42);
auto sptr = std::make_shared<int>(ptr);
delete ptr;  // ptr 被销毁
sptr->get();  // sptr 现在指向一个无效指针,因此 get() 会抛出异常

Practical case

The following is how to use smart pointers to manage Dynamically allocated array:

// 原始指针版本
int* arr = new int[10];  // 分配数组

// ...

delete[] arr;  // 释放数组

// 智能指针版本
std::unique_ptr<int[]> arr = std::make_unique<int[]>(10);  // 分配数组

// ...

// arr 在离开范围时自动释放

The smart pointer version is safer as it prevents memory leaks and dangling pointers.

The above is the detailed content of C++ smart pointers: a comprehensive analysis of their life cycle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn