Initializing a vector<BYTE> from existing BYTE array
-
Tuesday, February 05, 2008 5:47 PM
Hello,
I wish to initialize a vector<BYTE> from an existing BYTE array.
Simple example:
BYTE buf[] = {0,1,2,3,4,5};
std::vector<BYTE> vec(buf, buf + sizeof(buf)/sizeof(buf[0]));This sample works and does what I asked for. The only difference is that it copies the buffer. I want the vector to point to the original BYTE array and not allocate+copy it.
The reason behind this is because I have an old API that sends me a BYTE array (that was read from a file) and I want it in a vector. I can't afford duplicating the BYTE array that I receive because it can get to huge sizes.Thanks.
All Replies
-
Tuesday, February 05, 2008 7:07 PMModerator
There's no way to have a std::vector use an existing allocation of memory.
You could write a std::vector-like wrapper around an existing byte array (naturally, operations that would need to resize the array would have to be left out of the implementation).
There's also the class boost::array (http://www.boost.org/doc/html/array.html) that might be what you're looking for.
-
Tuesday, February 05, 2008 7:13 PMHi, thanks for the quick response.
Unfortunately boost::array is not what I'm looking for since it's size is constant.
I couldn't figure out what you meant by writing a vector-like wrapper class, can you please elaborate? -
Tuesday, February 05, 2008 7:33 PMModerator
What exactly are you expecting?
The size of BYTE buf[] = {0,1,2,3,4,5} is also constant - the only way it won't be constant is if you copy the data into dynamically allocated memory, which you've said you cannot do. If you need the full capability of std::vector, then you must copy the data, unless you can modifiy the code that's supplying the data to write into an existing std::vector (which it sounds like you also can't do).
-
Tuesday, February 05, 2008 8:23 PMModerator
Idan K wrote: Hi, thanks for the quick response.
I couldn't figure out what you meant by writing a vector-like wrapper class, can you please elaborate?Something like this:
Code Snippettemplate <typename T> class fixed_vector
{
private:
T* m_begin;
T* m_end;
public:
typedef T elem_type;
tyepdef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T& reference;
typedef const T& const_reference;
fixed_vector(T* begin, T* end)
: m_begin(begin), m_end(end)
{
}fixed_vector(T*begin, size_t size)
: m_begin(begin), m_end(begin+size)
{
}
size_t size() { return m_end - m_begin; }
iterator begin() { return m_begin; }
const_iterator begin() const { return m_begin; }
iterator end() { return m_end; }
const_iterator end() const { return m_end; }
reference operator[](size_t idx) { return m_begin[idx]; }
const_reference T& operator[](size_t idx) const { return m_begin[idex]; }
reference at(size_t idx)
{
if (idx > size())
throw std::out_of_range("fixed vector size exceeded");
return m_begin[idx];
}
const_reference at(size_t idx) const
{
if (idx > size())
throw std::out_of_range("fixed vector size exceeded");
return m_begin[idx];
}
reference front() { return m_begin[0]; }
const_reference front() const { return m_begin[0]; }
// ... and so on

