如何将现有数据表示为 std::vector
我必须将现有数据(已知大小的 unsigned char
内存区域)传递给需要 const std::vector
的库函数。有没有办法“欺骗”库函数,让它相信它在对现有数据进行操作时收到了向量?
我将旧版本的数据作为指针和大小,而不是作为
std::vector
。传统 C 代码通过 malloc() 分配内存并提供指针和大小。请不要建议接触遗留代码 - 到这句话结束时,我将不再是公司的员工。我不想创建临时向量并复制数据,因为内存吞吐量很大(> 5GB/秒)。
放置新创建向量 - 但第一个字节用于向量数据本身。我不能在内存区域之前使用几个字节 - 遗留代码没有想到这一点(见上文 - 内存区域是由 malloc() 分配的)。
更改第三方库是不可能的。它需要 const std::vectorstd::byte& - 不是跨度迭代器等。
看起来我别无选择,只能使用临时向量,但也许还有其他想法......我不在乎,但它是关于密集的视频处理,并且会有很多数据需要复制无缘无故。
I have to pass existing data (unsigned char
memory area with known size) to the library function expecting const std::vector<std::byte>&
. Is there any way to "fool" the library function to believe that it received a vector while operating on existing data?
I have data from the old legacy as a pointer and size, not as a
std::vector
. Legacy C code allocates memory by malloc() and provides pointer and size. Please do not suggest touching the legacy code - by the end of the phrase I'll cease to be an employee of the company.I don't want to create temporary vector and copy data because memory throughtput is huge (> 5GB/sec).
Placement new creates vector - but with the first bytes used for the vector data itself. I cannot use few bytes before the memory area - legacy code didn't expect that (see above - memory area is allocated by malloc()).
Changing third party library is out of question. It expects const std::vectorstd::byte& - not span iterators etc.
It looks that I have no way but to go with temporary vector but maybe there are other ideas... I wouldn't care but it is about intensive video processing and there will be a lot of data to copy for nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
