c++ 类和动态内存分配

在C++中,当类涉及动态内存分配时,需特别注意资源管理,以避免内存泄漏、悬空指针等问题。以下是关键点和示例代码:

核心原则

  1. 析构函数:负责释放动态分配的内存。
  2. 拷贝构造函数:实现深拷贝,复制数据而非指针。
  3. 拷贝赋值运算符:处理自赋值并实现深拷贝。
  4. 移动语义(C++11+):通过移动构造函数和移动赋值运算符提升性能。

示例:自定义字符串类

cpp 复制代码
#include <cstring>
#include <utility>

class MyString {
private:
    char* str;

public:
    // 构造函数
    explicit MyString(const char* s = "") {
        str = new char[std::strlen(s) + 1];
        std::strcpy(str, s);
    }

    // 析构函数
    ~MyString() {
        delete[] str;
    }

    // 拷贝构造函数(深拷贝)
    MyString(const MyString& other) {
        str = new char[std::strlen(other.str) + 1];
        std::strcpy(str, other.str);
    }

    // 拷贝赋值运算符(拷贝并交换)
    MyString& operator=(MyString other) {
        swap(other);
        return *this;
    }

    // 移动构造函数
    MyString(MyString&& other) noexcept : str(other.str) {
        other.str = nullptr;
    }

    // 移动赋值运算符
    MyString& operator=(MyString&& other) noexcept {
        if (this != &other) {
            delete[] str;
            str = other.str;
            other.str = nullptr;
        }
        return *this;
    }

    // 交换辅助函数
    void swap(MyString& other) noexcept {
        std::swap(str, other.str);
    }

    // 访问数据
    const char* c_str() const {
        return str;
    }
};

关键点解析

  1. 深拷贝与浅拷贝

    • 默认拷贝操作复制指针值(浅拷贝),导致多个对象共享同一内存。
    • 需手动实现拷贝构造函数和赋值运算符,分配新内存并复制内容(深拷贝)。
  2. 拷贝并交换惯用法

    • 赋值运算符通过传值调用拷贝构造函数生成临时对象。
    • 交换当前对象与临时对象的资源,临时对象析构时自动释放旧资源。
  3. 移动语义

    • 移动操作"窃取"资源,避免不必要的深拷贝。
    • 移动后源对象置于有效但未定义状态(通常置空指针)。
  4. 异常安全

    • 在分配新内存前不修改原对象状态,确保异常时对象仍有效。

使用智能指针简化管理

若使用 std::unique_ptr 管理资源,可减少手动内存管理,但需显式实现深拷贝:

cpp 复制代码
#include <memory>
#include <algorithm>

class MyArray {
private:
    std::unique_ptr<int[]> arr;
    size_t size;

public:
    MyArray(size_t s) : arr(std::make_unique<int[]>(s)), size(s) {}

    // 深拷贝构造函数
    MyArray(const MyArray& other) : arr(std::make_unique<int[]>(other.size)), size(other.size) {
        std::copy(&other.arr[0], &other.arr[size], &arr[0]);
    }

    // 拷贝赋值运算符(拷贝并交换)
    MyArray& operator=(MyArray other) {
        swap(other);
        return *this;
    }

    // 移动操作由 unique_ptr 自动处理
    MyArray(MyArray&&) noexcept = default;
    MyArray& operator=(MyArray&&) noexcept = default;

    void swap(MyArray& other) noexcept {
        std::swap(arr, other.arr);
        std::swap(size, other.size);
    }
};

总结

  • 手动管理内存时,必须实现析构函数、拷贝构造、拷贝赋值(移动操作可选但推荐)。
  • 使用智能指针 (如 std::unique_ptr)可自动管理释放,但深拷贝仍需手动实现。
  • 遵循规则:若定义了拷贝构造、拷贝赋值或析构函数,通常需同时考虑移动操作。

正确管理动态内存能有效避免资源泄漏和程序崩溃,是C++高效编程的关键技能。

相关推荐
薄荷撞~可乐2 分钟前
C#高并发与并行理解处理
开发语言·c#
孤廖7 分钟前
【算法磨剑:用 C++ 思考的艺术・Dijkstra 实战】弱化版 vs 标准版模板,洛谷 P3371/P4779 双题精讲
java·开发语言·c++·程序人生·算法·贪心算法·启发式算法
躯坏神辉11 分钟前
c++怎么读取文件里的内容和往文件里写入数据
c++
sali-tec12 分钟前
C# 基于halcon的视觉工作流-章33-矩状测量
开发语言·人工智能·算法·计算机视觉·c#
凯子坚持 c17 分钟前
Redis 核心数据结构:String 类型深度解析与 C++ 实战
数据结构·c++·redis
芝士小宇19 分钟前
2025.9.12Qtday2
c++·qt
码畜也有梦想20 分钟前
Maven中optional的作用
java·jenkins·maven
云和数据.ChenGuang39 分钟前
java常见SSL bug解决方案
java·bug·ssl
songx_9941 分钟前
leetcode29( 有效的括号)
java·数据结构·算法·leetcode
于樱花森上飞舞1 小时前
【java】常见排序算法详解
java·算法·排序算法