动态内存管理类StrVec的实现

实现一个动态内存管理类

  1. StrBlobvector<string>可以管理元素的内存.
  2. 实现一个vector<string>
allocator<>类是分配的原始内存,当调用alloc.destroy(first_free),
  • 按道理来说destroy执行对象的析构函数,string析构函数执行完后会回收内存,再访问该string就访问越界了
  • 但是如果在destroy()后访问string指针的内容,仍可以读取到,我觉得原因在于内存是由allocator类申请来的的,由alloc对象管理,即使string对象执行完析构函数.内存也不会回收

alloc_n_copy非常巧妙

  • 在原alloc对象申请原始内存,通过uninitialized_copy()来初始化
  • 支持vector<>的部分初始化

StrVec.h

#ifndef STRVEC_H
#define STRVEC_H

#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <initializer_list>
#include <algorithm>


class StrVec {
public:
    StrVec():elements(nullptr),first_free(nullptr),cap(nullptr) {}
    StrVec(std::initializer_list<std::string> i1);
    StrVec(const StrVec&);
    StrVec& operator=(const StrVec&);
    ~StrVec();
    StrVec(size_t);
    StrVec(size_t,const std::string&);

    void resize(size_t,const std::string&);
    void resize(size_t);
    void reserve(size_t);
    void push_back(std::string&);
    void push_back(std::string);
    std::string pop_back();
    size_t size() const { return first_free - elements; }
    size_t capacity() const { return cap - elements; }
    std::string* begin() const { return elements; }
    std::string* end() const { return first_free; }

private:
    static std::allocator<std::string> alloc;

    void chk_n_alloc();
    void free();
    void reallocate();
    void reallocate(size_t);
    std::pair<std::string*,std::string*>
        alloc_n_copy(const std::string*,const std::string*);

    void alloc_n_copy(size_t,const std::string&);

    std::string* elements;
    std::string* first_free;
    std::string* cap;
};
void StrVec::free() {
   /* if (elements)*/
        //for (auto it = first_free; it != elements;)
            /*alloc.destroy(--it);*/
    if (elements)
        std::for_each(elements,first_free,
            [](std::string& s){ alloc.destroy(&s);});
    alloc.deallocate(elements,cap - elements);
    std::cout << " ~StrVec() " << std::endl;
}
void StrVec::reallocate() {
    auto newcapacity = size() ? 2*size() : 1;
    auto newdata = alloc.allocate(newcapacity);

    auto dest = newdata;
    auto elem = elements;
    for (size_t i=0; i!=size(); ++i)
        alloc.construct(dest++,std::move(*elem++));
    free();

    elements = newdata;
    first_free = dest;
    cap = elements + newcapacity;
}

void StrVec::reallocate(size_t newcapacity) {
    auto newdata = alloc.allocate(newcapacity);

    auto dest = newdata;
    auto elem = elements;
    for (size_t i=0; i!=size(); ++i)
        alloc.construct(dest++,std::move(*elem++));
    free();

    elements = newdata;
    first_free = dest;
    cap = elements + newcapacity;
}

void StrVec::reserve(size_t n) {
    if (n > capacity()) reallocate(n);
}
void StrVec::resize(size_t n) {
    if (n > size()) {
        while (size() < n)
            push_back("");
    } else if (n < size()) {
        while (size() > n)
            alloc.destroy(--first_free);
    }
}
void StrVec::resize(size_t n,const std::string& s) {
    if (n > size()) {
        while (size() < n)
            push_back(s);
    }
}

void StrVec::chk_n_alloc() {
    if (size() == capacity())
        reallocate();
}
void StrVec::push_back(std::string& s) {
    chk_n_alloc();
    alloc.construct(first_free++,s);
}

void StrVec::push_back(std::string s) {
    chk_n_alloc();
    alloc.construct(first_free++,s);
}

std::pair<std::string*,std::string*>
StrVec::alloc_n_copy(const std::string* b,const std::string* c) {
    auto data = alloc.allocate(c-b);
    return {data,uninitialized_copy(b,c,data)};
}

StrVec::StrVec(const StrVec& rhs) {
    auto newdata = alloc_n_copy(rhs.begin(),rhs.end());
    elements = newdata.first;
    cap = first_free = newdata.second;
}

void StrVec::alloc_n_copy(size_t n,const std::string& s) {
    auto newdata = alloc.allocate(n);
    auto dest = newdata;
    for (auto i=0; i<n; ++i)
        alloc.construct(dest++,s);
    elements = newdata;
    first_free = dest;
    cap = elements + n;

}
StrVec::StrVec(size_t n) {
    alloc_n_copy(n,"");
}

StrVec::StrVec(size_t n,const std::string& s) {
    alloc_n_copy(n,s);
}

StrVec::~StrVec() { free(); }

StrVec& StrVec::operator=(const StrVec& rhs) {
    auto data = alloc_n_copy(rhs.begin(),rhs.end());
    free();
    elements = data.first;
    cap = first_free = data.second;
    return *this;
}
StrVec::StrVec(std::initializer_list<std::string> i1) {
    auto newdata = alloc_n_copy(i1.begin(),i1.end());
    elements = newdata.first;
    cap = first_free = newdata.second;
}
std::string StrVec::pop_back() {
    if (size()>=1) {
        std::string temp = *(--first_free);
        alloc.destroy(first_free);
        return temp;
    }
}
#endif

StrVec类的使用

main.cpp

#include <iostream>
#include "StrVec.h"
#include <string>

std::allocator<std::string> StrVec::alloc;

int main() {
    std::string test(" test string ");
    StrVec temp1(10,test);
    StrVec temp2 = {"a","ab","abc","abcd"};
    StrVec temp3(2);
    StrVec temp4 = temp2;
    for (auto i:temp1)
        std::cout << " i " << i << std::endl;
    for (auto i:temp2)
        std::cout << " i " << i << std::endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值