Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to use std::vector as a simple buffer?

Tags:

c++

std

stdvector

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:

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. 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.

like image 583
Roger Rowland Avatar asked Oct 23 '13 13:10

Roger Rowland


People also ask

Is std::vector fast?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Is it better to use array or vector?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.

Why is std :: array more efficient than std::vector if you know the number of elements you need?

Difference between std::vector and std::array in C++Vector is dynamic in nature so, size increases with insertion of elements. As array is fixed size, once initialized can't be resized. Vector occupies more memory. Array is memory efficient data structure.

What is faster array or vector C++?

The conclusion is that arrays of integers are faster than vectors of integers (5 times in my example). However, arrays and vectors are arround the same speed for more complex / not aligned data.


1 Answers

  1. 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).
  2. No, reserve is not necessary here; resize will automatically grow the capacity as necessary, in exactly the same way.
  3. Before C++03, technically not (but in practice yes). Since C++03, yes.

Incidentally, though, memcpy_s isn't the idiomatic approach here. Use std::copy instead. Keep in mind that a pointer is an iterator.

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.

like image 136
Sneftel Avatar answered Sep 20 '22 10:09

Sneftel