是否良好的做法是使用std :: vector作为一个简单的缓冲区? [英] Is it good practice to used std::vector as a simple buffer?
问题描述
我有一个应用程序正在对一些图像执行一些处理。
I have an application that is performing some processing on some images.
由于我知道宽度/高度/格式等(我这样做)只是定义一个缓冲区来存储像素数据:
Given that I know the width/height/format etc. (I do), and thinking just about defining a buffer to store the pixel data:
然后,而不是使用 new
和<$ c $在 unsigned char *
上保留一个单独的缓冲区大小的注意事项,我想要简化的事情, std :: vector
。
Then, rather than using new
and delete []
on an unsigned char*
and keeping a separate note of the buffer size, I'm thinking of simplifying things by using a std::vector
.
所以我要声明我的类如下:
So I would declare my class something like this:
#include <vector>
class MyClass
{
// ... etc. ...
public:
virtual void OnImageReceived(unsigned char *pPixels,
unsigned int uPixelCount);
private:
std::vector<unsigned char> m_pImageBuffer; // buffer for 8-bit pixels
// ... etc. ...
};
然后,当我收到一个新的图片详细信息),我可以调整向量(如果需要)和复制像素:
Then, when I received a new image (of some variable size - but don't worry about those details here), I can just resize the vector (if necessary) and copy the pixels:
void MyClass::OnImageReceived(unsigned char *pPixels, unsigned int uPixelCount)
{
// called when a new image is available
if (m_pImageBuffer.size() != uPixelCount)
{
// resize image buffer
m_pImageBuffer.reserve(uPixelCount);
m_pImageBuffer.resize(uPixelCount, 0);
}
// copy frame to local buffer
memcpy_s(&m_pImageBuffer[0], m_pImageBuffer.size(), pPixels, uPixelCount);
// ... process image etc. ...
}
$ b b
这对我来说似乎很好,我喜欢这个事实,我不必担心内存管理,但它提出了一些问题:
This seems fine to me, and I like that fact that I don't have to worry about the memory management, but it raises some questions:
- 这是
std :: vector
的有效申请吗?还是有更合适的容器吗? - 通过调用
reserve
和resize
? - 总是是底层内存是连续的,所以我可以使用
memcpy_s
如图所示?
- Is this a valid application of
std::vector
or is there a more suitable container? - Am I doing the right thing performance-wise by calling
reserve
andresize
? - Will it always be the case that the underlying memory is consecutive so I can use
memcpy_s
as shown?
任何其他意见,批评或建议都是非常欢迎的。
Any additional comment, criticism or advice would be very welcome.
推荐答案
- 当然,这会正常工作。你需要担心的一件事是确保缓冲区正确对齐,如果你的类依赖于一个特定的对齐方式;在这种情况下,您可能想使用数据类型本身的向量(如
float
)。 - 不, ;调整大小会根据需要自动增加容量。
- 在C ++ 03之前,技术上不是(但实际上是)。从C ++ 03开始,是的。
- Sure, this'll work fine. The one thing you need to worry about is ensuring that the buffer is correctly aligned, if your class relies on a particular alignment; in this case you may want to use a vector of the datatype itself (like
float
). - No, reserve is not necessary here; resize will automatically grow the capacity as necessary, in exactly the same way.
- Before C++03, technically not (but in practice yes). Since C++03, yes.
顺便提一下, memcpy_s
isn这里的惯用方法。请改用 std :: copy
。请记住,指针是一个迭代器。
Incidentally, though, memcpy_s
isn't the idiomatic approach here. Use std::copy
instead. Keep in mind that a pointer is an iterator.
这篇关于是否良好的做法是使用std :: vector作为一个简单的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!