copy buffer to std::vector

Started by
15 comments, last by rnlf_in_space 11 years, 7 months ago
It depends on what the OP is using it for. For something like a screenshot, I wouldn't bother trying to optimise too heavily, the code is run so rarely relative to other parts of the system. If the code is run more frequently, the bigger performance problem is probably the copying of data from the GPU and the associated pipeline stall.
Advertisement
Why bother? Where is it more work or less comfort to use a smart-pointer to an array instead of using a vector? It has nothing but advantages (you just cannot use .at() but who does anyways?)

Why bother? Where is it more work or less comfort to use a smart-pointer to an array instead of using a vector? It has nothing but advantages (you just cannot use .at() but who does anyways?)

You mean using a simple (smart) pointer has nothing but advantages over a data store that tracks its size automatically, can be easily resized, and can be optimized by the compiler when used with most standard algorithms to produce better code? You're probably right, except for all those places where you're wrong.

Certainly, if you want a raw chunk of memory for a fixed-length byte buffer it makes just as much sense to use a (smart) pointer and ::operator new (or, if you're stuck in the 1970's its grandfather, malloc()) as to use a std::vector. The minute you start doing anything more complicated than that, you're generally better off using an appropriate higher-level data structure.

Stephen M. Webb
Professional Free Software Developer

Bregma, yes. You're absolutely right. My comment is just for this situation (read a fixed-size buffer from a C library, do something like writing it to a file and free the buffer), where it is most unlikely that resizing or your off-the-shelf-C++-standard-library-algorithms are required. But how often do you sort() or erase_if() your pixel buffer data? It is most likely that the data will either be written directly to an image file or processed for post-effects. In that case there is really no advantage in using a full-fledged container.

I never said using vectors for anything but this general use case was a bad idea. I didn't say so, because this would in fact be seriously stupid.
Code doesn't exist in isolation, the OP may already have code for dealing with the vector. This might need to be modified to account for changing to a smart array.

The main general drawback is that the vector remembers its size, whereas the smart array does not. This probably is not an issue here, as the vector's size is likely insufficient anyway - it is actually a two dimensional array being hidden in a one dimensional vector.
For the necessities of my application using a std::vector to store pixels is just fine. I need to grab those pixels each time that the camera orientation/position (managed by the mouse) has changed. And so, each time that the mouse button is released the function runs, grab the pixels into a vector, sort and remove duplicate values (I use it later to do opengl picking based on colors). The operation takes less than 100ms, and is almost unnoticeable.
Interesting the use of the boost library, as I have some time I will try that as well, as I get some spare time...
Wow... I didn't think my examples of standard algorithms that I thought nobody would like to use with pixel data are just what you want to do with your pixel data. I withdraw my objections from a few post up ;-)

But the algorithms do of course also work with arrays. ;-)

This topic is closed to new replies.

Advertisement