本文介绍了C ++ STL向量保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已经测试stl向量与下面的代码:

I have tested on stl vector with code below:

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中有一个尖峰。推回。我怀疑有一个内存的重新分配。我想知道为什么是这样,因为我已经使用储备?我试图使用vectorv1(1000)声明该向量,它也给出了相同的行为。

I realized that for every 16 push_back, there is a spike in the v1.push_back. I suspect that there is a reallocation of memory. I am wondering why is it so since I already use the reserve? I tried to declared the vector using vectorv1(1000), it also gives the same behaviour.

顺便说一下,如果我将char增加到512, push_back,8 * 512提供约4k内存。这是与内存分页相关的问题吗?

By the way, if I increase the char into 512, it took just 8 push_back, 8 * 512 gives around 4k memory. Would that the issue related to memory paging?

谢谢。

推荐答案

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

Run this simple test and see if there are any allocations or deallocations you don't want or don't expect.

#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向量保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:15