468,512 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Initializing a vector<BYTE> from existing BYTE array

Hello,

I wish to initialize a vector<BYTE> from an existing BYTE array.

Simple example:

Expand|Select|Wrap|Line Numbers
  1. BYTE buf[] = {0,1,2,3,4,5};
  2. std::vector<BYTE> vec(buf, buf + sizeof(buf)/sizeof(buf[0]));
  3.  
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.
Feb 5 '08 #1
2 10124
gpraghuram
1,275 Expert 1GB
Hello,

I wish to initialize a vector<BYTE> from an existing BYTE array.

Simple example:

Expand|Select|Wrap|Line Numbers
  1. BYTE buf[] = {0,1,2,3,4,5};
  2. std::vector<BYTE> vec(buf, buf + sizeof(buf)/sizeof(buf[0]));
  3.  
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.
I think whenever you push an element to the vector it allocates and copies the object.
Maybe other members can add to this
Raghuram
Feb 6 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
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.
This is a contradiction. You can't use a vector to manage an array that's not in the vector.

Also, I don't understand your duplication problem. The BYTE array is on the heap so you copy it to a vector<BYTE> that's also on the heap and then delete the BYTE array.

That is, unless you are using the stack, which is a no-no in C++.

What is "huge" anyway ??
Feb 6 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by klaas | last post: by
3 posts views Thread by Alexandros | last post: by
4 posts views Thread by Jeff Paciga | last post: by
17 posts views Thread by Karl Ebener | last post: by
16 posts views Thread by Vince | last post: by
16 posts views Thread by call_me_anything | last post: by
11 posts views Thread by mathieu | last post: by
8 posts views Thread by barcaroller | last post: by
2 posts views Thread by vikasetrx | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.