是否可以在已分配的内存上初始化 std::vector? [英] Is it possible to initialize std::vector over already allocated memory?
问题描述
我的问题很简单,我很惊讶我找不到任何相关的东西.可能很容易或完全愚蠢(或者我无法搜索).
My question is fairly simple and I am quite surprised I can't find anything related. Probably it is easy or totally stupid (or I can't search).
正如标题所说,是否可以在已分配的内存上使用 std::vector
,因此它不会从一开始就分配新元素,而是使用给定的内容.我会想象它是这样的:
As the title says, is it possible to use std::vector
on already allocated memory, so it does not allocate new elements from start but uses what is given. I would imagine it as something like:
T1 *buffer = new T1[some_size];
std::vector<T2> v(buffer, some_size); // <- ofc doesn't work
相反的方法很简单并且(可能不漂亮但是)有效:
The opposite is quite simple and (maybe not pretty but) works:
std::vector<T2> v(some_size);
T1 *buffer = &v[0];
保证存储是连续的,所以和迭代器一样安全.
The storage is guaranteed to be continuous, so it is as safe as an iterator.
我的动机很简单.我传递了一些原始内存数据,即字节,并且因为我知道它们在其他一些地方的解释,所以我想将它们转换回有意义的东西.我可以做一个 reinterpret_cast
并使用普通的 c 样式数组,但我更喜欢 c++ 工具.
My motivation is quite simple. I pass around some raw memory data, i.e. bytes, and since I know their interpretation at some other places I would like to convert them back to something meaningful. I could do a reinterpret_cast
and use normal c-style array, but I prefer c++ facilities.
我觉得这应该是安全的,因为我们将 buffer
的所有权交给了 vector,因为它需要能够重新分配.
I get the feeling this should be safe given that we give up ownership of buffer
to vector, because it needs to be able to reallocate.
推荐答案
像这样.. 标准中的容器通常采用分配器.使用 c++11 的分配器特性,创建分配器非常容易,因为您不必在分配器中拥有所有成员.但是,如果使用旧版本的 C++,您将需要实现每个成员并进行重新绑定!
Like this.. Containers in the standard usually take an allocator. Using c++11's allocator traits, it is very easy to create an allocator as you don't have to have all the members in the allocator. However if using an older version of C++, you will need to implement each member and do the rebinding as well!
对于 Pre-C++11,您可以使用以下内容:
For Pre-C++11, you can use the following:
#include <iterator>
#include <vector>
#include <iostream>
template<typename T>
class PreAllocator
{
private:
T* memory_ptr;
std::size_t memory_size;
public:
typedef std::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;
PreAllocator(T* memory_ptr, std::size_t memory_size) throw() : memory_ptr(memory_ptr), memory_size(memory_size) {};
PreAllocator (const PreAllocator& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator (const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator& operator = (const PreAllocator<U>& other) {return *this;}
PreAllocator<T>& operator = (const PreAllocator& other) {return *this;}
~PreAllocator() {}
pointer address (reference value) const {return &value;}
const_pointer address (const_reference value) const {return &value;}
pointer allocate (size_type n, const void* hint = 0) {return memory_ptr;}
void deallocate (T* ptr, size_type n) {}
void construct (pointer ptr, const T& val) {new (ptr) T (val);}
template<typename U>
void destroy (U* ptr) {ptr->~U();}
void destroy (pointer ptr) {ptr->~T();}
size_type max_size() const {return memory_size;}
template<typename U>
struct rebind
{
typedef PreAllocator<U> other;
};
};
int main()
{
int my_arr[100] = {0};
std::vector<int, PreAllocator<int> > my_vec(PreAllocator<int>(&my_arr[0], 100));
my_vec.push_back(1024);
std::cout<<"My_Vec[0]: "<<my_vec[0]<<"\n";
std::cout<<"My_Arr[0]: "<<my_arr[0]<<"\n";
int* my_heap_ptr = new int[100]();
std::vector<int, PreAllocator<int> > my_heap_vec(PreAllocator<int>(&my_heap_ptr[0], 100));
my_heap_vec.push_back(1024);
std::cout<<"My_Heap_Vec[0]: "<<my_heap_vec[0]<<"\n";
std::cout<<"My_Heap_Ptr[0]: "<<my_heap_ptr[0]<<"\n";
delete[] my_heap_ptr;
my_heap_ptr = NULL;
}
对于 C++11,您可以使用以下内容:
For C++11, you can use the following:
#include <cstdint>
#include <iterator>
#include <vector>
#include <iostream>
template <typename T>
class PreAllocator
{
private:
T* memory_ptr;
std::size_t memory_size;
public:
typedef std::size_t size_type;
typedef T* pointer;
typedef T value_type;
PreAllocator(T* memory_ptr, std::size_t memory_size) : memory_ptr(memory_ptr), memory_size(memory_size) {}
PreAllocator(const PreAllocator& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator& operator = (const PreAllocator<U>& other) { return *this; }
PreAllocator<T>& operator = (const PreAllocator& other) { return *this; }
~PreAllocator() {}
pointer allocate(size_type n, const void* hint = 0) {return memory_ptr;}
void deallocate(T* ptr, size_type n) {}
size_type max_size() const {return memory_size;}
};
int main()
{
int my_arr[100] = {0};
std::vector<int, PreAllocator<int>> my_vec(0, PreAllocator<int>(&my_arr[0], 100));
my_vec.push_back(1024);
std::cout<<"My_Vec[0]: "<<my_vec[0]<<"\n";
std::cout<<"My_Arr[0]: "<<my_arr[0]<<"\n";
int* my_heap_ptr = new int[100]();
std::vector<int, PreAllocator<int>> my_heap_vec(0, PreAllocator<int>(&my_heap_ptr[0], 100));
my_heap_vec.push_back(1024);
std::cout<<"My_Heap_Vec[0]: "<<my_heap_vec[0]<<"\n";
std::cout<<"My_Heap_Ptr[0]: "<<my_heap_ptr[0]<<"\n";
delete[] my_heap_ptr;
my_heap_ptr = nullptr;
}
注意两个分配器的区别!这将适用于堆缓冲区/数组和堆栈缓冲区/数组.它也适用于大多数容器.使用 Pre-C++11 版本更安全,因为它将向后兼容并使用更多容器(即:std::List
).
Notice the difference between the two allocators! This will work with both heap buffers/arrays and stack buffer/arrays. It will also work with most containers. It is safer to use the Pre-C++11 version because it will be backwards compatible and work with more containers (ie: std::List
).
您可以将分配器放在标题中,并在任何项目中尽可能多地使用它.如果您想使用 SharedMemory
或任何已分配的缓冲区,这是很好的.
You can just place the allocator in a header and use it as much as you want in any projects. It is good if you want to use SharedMemory
or any buffer that is already allocated.
警告:不要同时为多个容器使用相同的缓冲区!缓冲区可以重复使用,但要确保没有两个容器同时使用它.
WARNING: DO NOT use the same buffer for multiple containers at the same time! A buffer can be reused but just make sure no two containers use it at the same time.
示例:
int my_arr[100] = {0};
std::vector<int, PreAllocator<int> > my_vec(PreAllocator<int>(&my_arr[0], 100));
std::vector<int, PreAllocator<int> > my_vec2(PreAllocator<int>(&my_arr[0], 100));
my_vec.push_back(1024);
my_vec2.push_back(2048);
std::cout<<"My_Vec[0]: "<<my_vec[0]<<"\n";
std::cout<<"My_Arr[0]: "<<my_arr[0]<<"\n";
上面的输出是2048!为什么?因为最后一个向量覆盖了第一个向量的值,因为它们共享相同的缓冲区.
The output of the above is 2048! Why? Because the last vector overwrote the values of the first vector since they share the same buffer.
这篇关于是否可以在已分配的内存上初始化 std::vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!