我可以使用std::向量作为预分配(原始)数组的门面吗
Can I use an std::vector as a facade for a pre-allocated (raw) array?
我从DirectX获取了一个存储顶点信息的内存位置。处理顶点信息的一种非常方便的方法是使用std::vector<>包含顶点信息的结构的。
假设我有一个指向大缓冲区的指针,我可以使用std::向量来管理缓冲区中的元素吗?构造一个std::向量会导致它有自己的地址,这不是我真正想要的。我能以某种方式使用新的操作员位置吗?
可以。使用自定义分配器。在此分配器中返回DirectX内存的地址。
下面是一个完整的例子,基于自定义C++STL分配器的令人信服的例子的答案?。此解决方案在分配器中使用placement new。
#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;
}
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));
}
相关文章:
- 将浮动的heightmap数组导出为16位原始值
- 复制后删除原始数组指针将前 3 个字节设置为 0
- 检查反向数组是否与原始数组相同
- C++将原始字符数组存储在元组中
- 如何将数组传递给函数,以便在更改该函数中的数组值时原始数组的值不会更改?
- 从原始指针(衰减的 C 样式数组)和大小生成范围::视图
- 如何在C++中将 std::vector 移动到原始数组中
- 头文件会导致函数调用后原始数组出现问题
- 初始化std :: unique_ptr作为原始数组指针的初始化
- std::string 参数是如何从原始字符数组文本传递的
- 原始数组和 std::array 在 clang++ 和 VC++ 上的不同迭代器行为
- 使用 Python、C++ 和 pybind11 返回并传递原始 POD 指针(数组)
- 从C 到C#调用INT []显示随机大数字,而不是原始数组成员
- 我可以使用std ::分配的分配的原始数组
- 使用 std::sort 和原始指针数组?
- 如何在C++的所有函数中编辑原始数组
- 如何在被调用后保留数组的原始值
- 排序后如何将二维数组重置为原始形式
- STL 算法 + c'tor 不再适用于指向 C 数组的原始指针,因为指向一个过去的末端
- 将数组的原始指针施放为unique_ptr