问题描述
我发现,用std :: vector来管理一系列映射内存(来自glMapBuffer)是一个好主意。
it occurred to me that it would be a good idea to manage a range of mapped memory (from glMapBuffer) with a std::vector.
// map data to ptr
T* dataPtr = (T*)glMapBuffer(this->target, access);
[... debug code ...]
// try to construct a std::vector from dataPtr
T* dataPtrLast = dataPtr + size;
mappedVector = new std::vector<T>(dataPtr, dataPtrLast);
问题是内存范围不会直接使用,而是复制到向量中。
the problem is that the memory range won't be used directly but it is copied into the vector.
我的问题是:是否可以使向量只使用映射的内存范围。 (理想情况下,在调整大小/保留时抛出异常)
或者是否有其他标准容器可以实现这一点?
My question would be: is it possible to make the vector just 'use' the mapped memory range. (and ideally throw exceptions on resize/reserve)Or is there any other standard container that would accomplish this?
/ p>
Kind Regards,Florian
推荐答案
不,很好。这段代码永远不会工作。例如,您可以更改MapBuffer并中断向量中的大小/容量值。您可以推入向量并导致访问冲突/分段故障。您可能会导致调整大小,销毁缓冲区。而且,根本上,如果它已经在一个连续的数组,有什么好处?您可以为固定长度的数组滚动自定义容器。
No, and for good reason. This code would never work. For example, you could alter the MapBuffer and break the size/capacity values inside the vector. You could push into the vector and cause an access violation/segmentation fault. You could cause a resize, destroying the buffer. And, fundamentally, if it's already within a contiguous array, what's the benefit? You could roll a custom container for fixed length arrays, I guess.
特别!如果你已经有一对指针来作为迭代器。
Especially! if you already have a pair of pointers to act like iterators.
这篇关于使用std :: vector管理映射内存(glMapBuffer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!