c++ - 我可以使用 std::vector 作为预分配(原始)数组的外观吗?

标签 c++ vector memory-address facade

我已经从 DirectX 获得了一个内存位置,其中存储了我的顶点信息。处理顶点信息的一种极其方便的方法是使用包含顶点信息的结构的 std::vector<>。

鉴于我有一个指向大缓冲区的指针,我可以使用 std::vector 来管理缓冲区中的元素吗?定期构造一个 std::vector 会导致它有自己的地址,这并不是我真正想要的。我能以某种方式使用新的运算符放置吗?

最佳答案

是的,你可以。使用 custom allocator .在此分配器中,您的 DirectX 内存的返回地址。

这是一个基于 Compelling examples of custom C++ STL allocators? 回答的完整示例.此解决方案在分配器中使用新放置。

#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;

}

关于c++ - 我可以使用 std::vector 作为预分配(原始)数组的外观吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807192/

相关文章:

c++ - C++中合并排序算法的怪异行为

c++ - 从原始数据就地创建 std::vector

c++ - 旋转对象以匹配 vector

c++ - 在 C++ 中使用它在内存中的起始地址编辑字符串

rust - 将 char 定位在特定的内存地址

c - double 型矩阵 [n]x[n] 传输至 ASSEMBLY

c++ - 使用 g++ 的成员函数的 undefined reference

c++ - 对函数类型的右值引用

c++ - 段错误(核心转储)C++,因为我使用了大量内存

r - 如何合并2个向量交替索引?