ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

C++ Primer 5th笔记(chap 12)动态内存 allocator类

2021-02-13 10:29:17  阅读:235  来源: 互联网

标签:12 vi 5th 42 allocate chap 内存 allocator size


1. 标准库allocator类及其算法

算法说明
allocator <.T> a定义了一个名为a的allocator对象,他可以为类型T的对象分配内存
a.allocate(n)分配一段原始的、未构造的内存,保存n个类型为T的对象
a.deallocate(n)释放从T*指针p中地址开始的内存,这块内存保存了n个类型为T的对象;p必须是一个先前由allocator返回的指针,且n必须是p创建时所要求的大小。调用dealocator之前,用户必须对每个在这块内存中创建的对象调用destroy
a.construct(p,args)p必须是一个类型为T*的指针,指向一块原始内存;arg被传递给类型为T的构造函数,用来在p指向的内存中构造一个对象
a.destroy§p为T*类型的指针,此算法对p指向的对象执行析构函数

注意:

    const size_t n = 100;
    allocator<string> allocStr;      // object that can allocate strings
    auto p = allocStr.allocate(n);   // allocate n unconstructed strings

    //cout << *p << endl;

    auto q = p; // q will point to one past the last constructed element
    allocStr.construct(q++);         // *q is the empty string
    cout << *(q - 1) << endl;

    allocStr.construct(q++, 10, 'c'); // *q is cccccccccc
    cout << *(q - 1) << endl;

    //allocStr.construct(q++, "hi");    // *q is hi!
    //cout << *(q - 1) << endl;

    int nCount = 0;
    cout << *p << endl;  // ok: uses the string output operator
    while (q != p) {
        nCount++;
        allocStr.destroy(--q);  // free the strings we actually allocated
    }
    cout << nCount << endl;  // ok: uses the string output operator 

    allocStr.deallocate(p, n);  // return the memory we allocated

    p = allocStr.allocate(n);   // allocate n unconstructed strings
    string s;
    q = p;                   // q points to the memory for first string
    ifstream in("E:/temp/storyDataFile");
    while (in >> s && q != p + n)
        allocStr.construct(q++, s); // construct only as many strings as we need
    size_t size = q - p;         // remember how many strings we read

    // use the array
    cout << "read " << size << " strings" << endl;

    for (q = p + size - 1; q != p; --q)
        allocStr.destroy(q);         // free the strings we allocated
    allocStr.deallocate(p, n);       // return the memory we allocated

    in.close();
    in.open("E:/temp/storyDataFile");
    p = new string[n];            // construct n empty strings
    q = p;                        // q points to the first string
    while (in >> s && q != p + n)
        *q++ = s;                 // assign a new value to *q
    size = q - p;                 // remember how many strings we read

    cout << "read " << size << " strings" << endl;

2. "copy和填充未初始化的内存"算法

算法说明
uninitialized_copy(b,e,b2)将迭代器b和e之间的输入,拷贝到迭代器b2指定的未构造的原始内存中,b2指向的内存必须足够大,能够容纳输入序列中元素的拷贝
uninitialized_copy_n(b,n,b2)同上,从b开始拷贝n个元素到b2
uninitialized_fill(b,e,t)在迭代器b和e指定的原始内存范围中创建对象,对象的值,均为t的拷贝
uninitialized_fill_n(b,n,t)从b指向的内存地址开始创建n个对象,b必须指向足够大的内存

使用示例:

vector<int> vi{ 1,2,3,4,5,6,7,8,9 };
  
allocator<int> alloc;

// allocate twice as many elements as vi holds
auto p = alloc.allocate(vi.size() * 2);

// construct elements starting at p as copies of elements in vi
auto q = uninitialized_copy(vi.begin(), vi.end(), p);
 
// initialize the remaining elements to 42
uninitialized_fill_n(q, vi.size(), 42);

for (size_t i = 0; i != vi.size(); ++i)
cout << *(p + i) << " ";
cout << endl;

for (size_t i = 0; i != vi.size(); ++i)
cout << *(q + i) << " ";
cout << endl;

alloc.deallocate(p, vi.size());

retsult:

1 2 3 4 5 6 7 8 9
42 42 42 42 42 42 42 42 42

标签:12,vi,5th,42,allocate,chap,内存,allocator,size
来源: https://blog.csdn.net/thefist11cc/article/details/113799485

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有