自定义Shared_ptr和Unique_ptr,引用计数和内存管理

本文深入探讨了智能指针的概念,包括共享指针(Shared_ptr)和独占指针(Unique_ptr)的实现原理与使用场景。通过代码示例,详细解释了智能指针如何管理资源,防止内存泄漏,以及它们在C++现代编程中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shared_ptr.h

#pragma once

template <typename T>
class Shared_ptr {
public:
    Shared_ptr();
    Shared_ptr(T *p);
    Shared_ptr(const Shared_ptr &sp);
    Shared_ptr &operator=(const Shared_ptr &sp);
    T &operator*();
    T &operator*() const;
    ~Shared_ptr();
private:
    T *ptr;
    unsigned *count;
};

template <typename T>
Shared_ptr<T>::Shared_ptr()
    : ptr(nullptr), count(nullptr) {}

template <typename T>
Shared_ptr<T>::Shared_ptr(T *p)
    : ptr(p), count(new unsigned(1)) {}

template <typename T>
Shared_ptr<T>::Shared_ptr(const Shared_ptr<T> &sp)
    : ptr(sp.ptr), count(sp.count) {
        ++*count;
}

template <typename T>
Shared_ptr<T> &Shared_ptr<T>::operator=(const Shared_ptr &sp) {
    --*count;
    if (0 == count) {
        delete ptr;
        delete count;
        ptr = nullptr;
        count = nullptr;
    }
    ptr = sp.ptr;
    count = sp.count;
    ++*count;

    return *this;
}

template <typename T>
T &Shared_ptr<T>::operator*() {
    return *ptr;
}

template <typename T>
T &Shared_ptr<T>::operator*() const {
    return *ptr;
}

template <typename T>
Shared_ptr<T>::~Shared_ptr() {
    if (count && --*count == 0) {
        delete ptr;
        delete count;
        ptr = nullptr;
        count = nullptr;
    }
}

Unique_ptr.h

#pragma once

template <typename T>
class Unique_ptr {
public:
    Unique_ptr();
    Unique_ptr(T *ptr);
    Unique_ptr(const Unique_ptr &up) = delete;
    ~Unique_ptr();
    Unique_ptr &operator=(const Unique_ptr &up) = delete;
    T *release();
    void reset(T *np);
    T &operator*();
    T &operator*() const;
private:
    T *ptr;
};

template <typename T>
Unique_ptr<T>::Unique_ptr()
    : ptr(nullptr) {}

template <typename T>
Unique_ptr<T>::Unique_ptr(T *ptr)
    : ptr(ptr) {}

template <typename T>
T *Unique_ptr<T>::release() {
    T *q = ptr;
    ptr = nullptr;
    return q;
}

template <typename T>
void Unique_ptr<T>::reset(T *np) {
    if (ptr) {
        delete ptr;
    }
    ptr = np;
}

template <typename T>
Unique_ptr<T>::~Unique_ptr() {
    if (ptr) {
        delete ptr;
    }
}

template <typename T>
T &Unique_ptr<T>::operator*() {
    return *ptr;
}

template <typename T>
T &Unique_ptr<T>::operator*() const {
    return *ptr;
}

main.cpp

#include <iostream>
#include <string>
#include "shared_ptr.h"
#include "unique_ptr.h"

using namespace std;

struct obj {
    obj(int n, string s)
        : num(n), str(s) {}

    int num = 0;
    string str;
};

int main() {
    auto p = new obj(1, "hello");
    Shared_ptr<obj> o(p);
    Shared_ptr<obj> t(o);

    cout << (*t).num << endl;
    cout << (*t).str << endl;

    auto q = new obj(2, "apple");
    Unique_ptr<obj> n(q);
    cout << (*n).num << endl;
    cout << (*n).str << endl;
    auto w = new obj(3, "map");
    n.reset(w);
    cout << (*(n.release())).str << endl;

    return 0;
}

析构函数的析构条件特别重要,是指向数据的指针不为NULL,并且共享指针的引用计数减1为0时,才析构数据。

未完成工作:添加删除器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值