-->

我可以使用的std ::向量作为幌子进行预分配(原始)阵列?(Can I use an std::v

2019-08-16 21:14发布

我已经获得来自DirectX的一个内存位置我的顶点信息被存储。 处理顶点信息的极其简便的方法是使用含有顶点信息的结构体的一个std ::矢量<>。

既然我有一个指向一个大的缓冲区,我可以用一个std ::向量来管理缓冲区中的元素? 构建一个std ::矢量经常导致它有它自己的地址,这是不是真的我想要的。 我可以使用运营商定位new不知何故?

Answer 1:

是的你可以。 使用自定义分配器 。 在你的DirectX内存这个分配器返回地址。

这是基于从回答的完整examlpe 定制的C ++ STL分配器的令人信服的例证? 。 该解决方案使用放置在分配新。

#include <memory>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class placement_memory_allocator: public std::allocator<T>
{
    void* pre_allocated_memory;
public:
    typedef size_t size_type;
    typedef T* pointer;
    typedef const T* const_pointer;

    template<typename _Tp1>
    struct rebind
    {
            typedef placement_memory_allocator<_Tp1> other;
    };

    pointer allocate(size_type n, const void *hint=0)
    {
            char* p = new(pre_allocated_memory)char[n * sizeof(T)];
            cout << "Alloc " << n * sizeof(T) << " bytes @" << hex << (void*)p <<endl;
            return (T*)p;
    }

    void deallocate(pointer p, size_type n)
    {
            cout << "Dealloc " << n << " bytes @" << hex << p << endl;
            //delete p;
    }

    placement_memory_allocator(void* p = 0) throw(): std::allocator<T>(), pre_allocated_memory(p) { cout << "Hello allocator!" << endl; }
    placement_memory_allocator(const placement_memory_allocator &a) throw(): std::allocator<T>(a) {pre_allocated_memory = a.pre_allocated_memory;}
    ~placement_memory_allocator() throw() { }
};

class MyClass
{   
    char empty[10];
    char* name;
public:
    MyClass(char* n) : name(n){ cout << "MyClass: " << name << " @" << hex << (void*)this << endl; }
    MyClass(const MyClass& s){ name = s.name; cout << "=MyClass: " << s.name << " @" << hex << (void*)this << endl; }
    ~MyClass(){ cout << "~MyClass: " << name << " @" << hex << (void*)this <<  endl; }
};

int main()
{
    // create allocator object, intialized with DirectX memory ptr.
    placement_memory_allocator<MyClass> pl(DIRECT_X_MEMORY_PTR);
    //Create vector object, which use the created allocator object.
    vector<MyClass, placement_memory_allocator<MyClass>> v(pl);
    // !important! reserve all the memory of directx buffer.
    // try to comment this line and rerun to see the difference
    v.reserve( DIRECT_X_MEMORY_SIZE_IN_BYTES / sizeof(MyClass));

    //some push backs.
    v.push_back(MyClass("first"));
    cout << "Done1" << endl;
    v.push_back(MyClass("second"));
    cout << "Done1" << endl;
    v.push_back(MyClass("third"));
    cout << "Done1" << endl;

}


Answer 2:

的元素std::vector动态地在堆上(具有分配的new通过std::vector本身)以这样的方式使得它们总是在存储器中连续。 所以,如果您的结构vertex ,使用std::vector<vertex>是不是你想要的东西作为载体的元素不会被位于您大的缓冲区。

你可以使用std::vector<vertex*>然而,像例如:

vertex* bigBuffer;     // provided by DirectX
size_t bigBufferLen;   // provided by DirectX

std::vector<vertex*> array;
for (size_t i = 0; i < bigBufferLen; ++i)
{
    array.push_back(bigBuffer + i * sizeof(vertex));
}


文章来源: Can I use an std::vector as a facade for a pre-allocated (raw) array?