C++ STL vector 保留

标签 c++ stl

我已经用下面的代码在 STL vector 上进行了测试:

struct structA{
   char charArray[256];
}

structA a;
..assign 256 characters to a.charArray

vector<structA> v1;
v1.reserve(1000);

for(int i=0; i<1000; i++){
   v1.push_back(a);
}

我意识到对于每 16 个 push_back,v1.push_back 中就会出现一个尖峰。我怀疑内存重新分配。我想知道为什么会这样,因为我已经使用了储备金?我尝试使用 vectorv1(1000) 声明 vector ,它也给出了相同的行为。

顺便说一下,如果我将 char 增加到 512,它只需要 8 个 push_back,8 * 512 提供大约 4k 内存。这个问题与内存分页有关吗?

谢谢。

最佳答案

运行这个简单的测试,看看是否有任何您不想要或不期望的分配或解除分配。

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>

template <class T> class my_allocator;

// specialize for void:
template <> class my_allocator<void> {
public:
    typedef void*       pointer;
    typedef const void* const_pointer;
    // reference to void members are impossible.
    typedef void value_type;
    template <class U> struct rebind { typedef my_allocator<U>    other; };
};

template <typename T> class my_allocator : public std::allocator<T> {
public:
    typedef size_t    size_type;
    typedef ptrdiff_t difference_type;
    typedef T*        pointer;
    typedef const T*  const_pointer;
    typedef T&        reference;
    typedef const T&  const_reference;
    typedef T         value_type;

    template <class U> 
    struct rebind { 
        typedef my_allocator<U> other; 
    };

    my_allocator() throw() 
    {
    }

    my_allocator(const my_allocator& to_copy) throw() 
    { 
    }

    template <class U> 
    my_allocator(const my_allocator<U>& to_copy) throw()
    {
    }

    ~my_allocator() throw()
    {
    }

    pointer address(reference x) const
    {
        return std::allocator<T>::address(x);
    }

    const_pointer address(const_reference x) const
    {
        return std::allocator<T>::address(x);
    }

    pointer allocate(size_type s1, typename std::allocator<void>::const_pointer hint = 0)
    {
        size_t block_size = s1 * sizeof (T);
        std::cout << "allocated, bytes: " <<  block_size << "\n";
        return std::allocator<T>::allocate(s1, hint);
    }

    void deallocate(pointer p, size_type n)
    {
        size_t block_size = n * sizeof (T);
        std::cout << "deallocated, bytes: " <<  block_size << "\n";
        std::allocator<T>::deallocate(p, n);
    }

    size_type max_size() const throw()
    {
        return std::allocator<T>::max_size();
    }

    void construct(pointer p, const T& val)
    {
        std::allocator<T>::construct(p, val);
    }

    void destroy(pointer p)
    {
        std::allocator<T>::destroy (p);
    }
};


struct structA{
    char charArray[256];
};

int main()
{
    structA a;

    std::cout << "Test 1, with reserve\n";
    {
        std::vector<structA, my_allocator<structA> > v1;
        v1.reserve(1000);
        for(int i=0; i<1000; i++){
            v1.push_back(a);
        }
    }
    std::cout << "Test 1, done\n";

    std::cout << "Test 2, without reserve\n";
    {
        std::vector<structA, my_allocator<structA> > v1;
        for(int i=0; i<1000; i++){
            v1.push_back(a);
        }
    }
    std::cout << "Test 2, done\n";

    return 0;
}

关于C++ STL vector 保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984588/

相关文章:

c++ - 如何调试 STL/C++ 的 GCC/LD 链接过程

c++ - 使用 mongodb c++ 驱动程序查询嵌套的 BSON 文档

c++ - 析构函数的名称是否符合标准?

c++ - 企业架构师错误 : Unexpected symbol fix?

c++ - 使用STL算法计算绝对值之和

C++ Hooking kernel32.dll OpenProcess 走弯路

使用STL和Arima的R预测季节和数据趋势

c++ - 关于c++ vector中for_each的一个问题

c++ - 如何创建节点结构类型的 Min STL priority_queue

c++ - 成对地遍历一个 vector ,三元组,