C/C++内存管理(实战)

类代码

代码用到两个类,没什么特殊之处,主要是C++代码类还是很重要的知识点,内存管理肯定要涉及到,而不是仅仅的基本数据类型。
基类Person:

#include <memory>  //allocator类
#include <string>
#include <new>  //placement new
#include <iostream>

using namespace std;

class Person
{
public:
    Person() = default;
    Person(const string &name) : m_name(name)
    {
    }
    //委托构造函数
    Person(const string &name, const string &sex) : Person(name)
    {
        m_sex = sex;
    }
    //析构函数
    virtual ~Person()
    {
        cout << __LINE__ << "@" << __FUNCTION__ << endl;
    }

    virtual const string &getName() const
    {
        return m_name;
    }
    virtual const string &getSex() const
    {
        return m_sex;
    }

private:
    string m_name;
    string m_sex;
};

子类Worker

class Worker : public Person
{
    //声明使用基类构造函数(继承构造函数)
    // using Person::Person;

public:
    Worker() = default;
    ~Worker()
    {
        cout << __LINE__ << "@" << __FUNCTION__ << endl;
    }

    Worker(const string &name) : m_name(name)
    {
    }
    //委托构造函数
    Worker(const string &name, const string &sex) : Worker(name)
    {
        m_sex = sex;
    }
    Worker(const string &name, const int age) : m_name(name), m_age(age)
    {
    }

    Worker(const string &name, const string &sex, const int age) : m_name(name), m_sex(sex), m_age(age)
    {
    }
    const string &getName() const override
    {
        return m_name;
    }
    const string &getSex() const override
    {
        return m_sex;
    }
    //年龄
    const int getAge() const
    {
        return m_age;
    }

    friend ostream &operator<<(ostream &os, Worker &wk);
    friend ostream &operator<<(ostream &os, Worker *pWk);

private:
    string m_name;
    string m_sex;
    int m_age = 0;
};

友元函数

两个友元函数用于打印数据信息。

ostream &operator<<(ostream &os, Worker &wk)
{
    os << wk.getName() << ", " << wk.getSex() << ", " << wk.getAge() << endl;
    return os;
}
ostream &operator<<(ostream &os, Worker *pWk)
{
    os << pWk->m_name << ", " << pWk->m_sex << ", " << pWk->m_age << endl;
    return os;
}

allocator类基本使用

示例 1:用于string类型

 allocator<string> alloc;
    string *p = alloc.allocate(100);
    cout << "&p:" << p << ", &(p+1):" << p + 1 << endl;
    string *q = p;
    //构造三个string对象
    alloc.construct(q++, "he is wang!");
    alloc.construct(q++, "he is zhao!");
    alloc.construct(q++, 10, 'w');
    //打印构造的三个对象
    for (int i = 0; i < 3; ++i)
    {
        cout << *(p + i) << endl;
    }
    //析构三个对象
    while (q != p)
    {
        // q指向的是最后一个对象之后的位置,因此析构这里必须用前置递减运算符
        alloc.destroy(--q);
    }
    //释放allocator对象申请的内存空间
    alloc.deallocate(p, 100);

运行结果是这样的:

示例 2:用于自定义类型

allocator<Worker> alloc2;
    Worker *ptr = alloc2.allocate(100);
    Worker *qtr = ptr;
    alloc2.construct(qtr++, "wang", "man", 22);
    alloc2.construct(qtr++, "wu", "woman");
    alloc2.construct(qtr++, "");
    alloc2.construct(qtr++, "hu", 22);
    cout << ptr[0] << endl; //验证表名ptr[0]与*(ptr)效果相同
    cout << ptr[1] << endl;
    cout << ptr[2] << endl;
    cout << ptr[3] << endl;
    //定位构造(placement new)
    Worker *p1 = new (ptr + 4) Worker("ximen", "man", 40);
    Worker *p2 = new (ptr + 5) Worker("dugu", "man", 50);
    cout << ptr[4] << endl;
    cout << ptr[5] << endl;
    //定位构造的两个对象也需要析构
    qtr++;
    qtr++;
    while (qtr != ptr)
    {
        alloc2.destroy(--qtr);
    }
    //释放申请的内存
    alloc2.deallocate(ptr, 100);

运行结果:

posted @ 2022-09-03 15:32  时间的风景  阅读(36)  评论(0编辑  收藏  举报