将std :: vector用作简单缓冲区是一种好习惯吗? [英] Is it good practice to use std::vector as a simple buffer?

查看:84
本文介绍了将std :: vector用作简单缓冲区是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在对某些图像进行某些处理的应用程序.

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:

然后,而不是在 unsigned char * 上使用 new delete [] ,并单独记录缓冲区大小,我我正在考虑使用 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. ...
}

这对我来说似乎很好,我喜欢我不必担心内存管理的事实,但这引起了一些问题:

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:

  1. 这是 std :: vector 的有效应用程序,还是有一个更合适的容器?
  2. 我是否通过调用 reserve resize 来做正确的事情?
  3. 底层存储器是连续的,是否总是这样,所以我可以使用 memcpy_s ,如图所示?
  1. Is this a valid application of std::vector or is there a more suitable container?
  2. Am I doing the right thing performance-wise by calling reserve and resize?
  3. 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.

推荐答案

  1. 好的,这会很好的.您需要担心的一件事是,如果您的类依赖于特定的对齐方式,则确保缓冲区正确对齐;在这种情况下,您可能需要使用数据类型本身的向量(例如 float ).
  2. 不,这里不需要保留;调整大小将以完全相同的方式根据需要自动增加容量.
  3. 在C ++ 03之前,从技术上讲不是(但实际上是).从C ++ 03开始,是的.

尽管如此, memcpy_s 并不是这里惯用的方法.请使用 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.

从C ++ 17开始, std :: byte 是不透明类型存储的惯用单位,例如您在此处使用的.当然, char 仍然可以使用,但是允许不安全的用法(如 char !),而 byte 则不能.

Starting in C++17, std::byte is the idiomatic unit of opaquely typed storage such as you are using here. char will still work, of course, but allows unsafe usages (as char!) which byte does not.

这篇关于将std :: vector用作简单缓冲区是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆